You could use combination of zip and Counter
a = ["abcd"]
b = ["a5c1"]
c = ["b51d"]
from collections import Counter
zippedList = list[zip[a,b,c]]
print["zipped: {}".format[zippedList]]
final = ""
for x in zippedList:
countLetters = Counter[x]
print[countLetters]
final += countLetters.most_common[3][0][0]
print["output: {}".format[final]]
output:
zipped: [['a', 'a', 'b'], ['b', '5', '5'], ['c', 'c', '1'], ['d', '1', 'd']]
Counter[{'a': 2, 'b': 1}]
Counter[{'5': 2, 'b': 1}]
Counter[{'c': 2, '1': 1}]
Counter[{'d': 2, '1': 1}]
output: a5cd
Suppose we have a list of numbers called nums, we have to find the number of elements that are present in the correct indices, when the list was to be sorted.
So, if the input is like [2, 8, 4, 5, 11], then the output will be 2, as the elements 2 and 11 are in their correct positions. The sorted sequence will be [2, 4, 5, 8, 11]
To solve this, we will follow these steps −
- s := sort the list nums
- count := 0
- for i in range 0 to size of nums, do
- if s[i] is same as nums[i], then
- count := count + 1
- if s[i] is same as nums[i], then
- return count
Let us see the following implementation to get better understanding −
Example
Live Demo
class Solution: def solve[self, nums]: s = sorted[nums] count = 0 for i in range[len[nums]]: if s[i] == nums[i]: count += 1 return count ob = Solution[] print[ob.solve[[2, 8, 4, 5, 11]]]
Input
[2, 8, 4, 5, 11]
Output
2
Updated on 05-Oct-2020 06:39:45
- Related Questions & Answers
- Program to count number of intervals which are intersecting at given point in Python
- 8085 program to count number of elements which are less than 0A
- Program to find number of elements in A are strictly less than at least k elements in B in Python
- Program to count number of elements present in a set of elements with recursive indexing in Python
- Program to count number of sublists with exactly k unique elements in Python
- Program to count number of ways to win at most k consecutive games in Python
- Program to return number of smaller elements at right of the given list in Python
- Program to count number of elements in a list that contains odd number of digits in Python
- Program to count number of palindromic substrings in Python
- Program to count number of unhappy friends in Python
- Program to count number of homogenous substrings in Python
- Program to count number of nice subarrays in Python
- Check if given array is almost sorted [elements are at-most one position away] in Python
- Program to count number of word concatenations are there in the list in python
- Python Program to repeat elements at custom indices
The count[] is a built-in function in Python. It will return the total count of a given element in a string. The counting begins from the start of the string till the end. It is also possible to specify the start and end index from where you want the search to begin. In this Python tutorial, you will learn: Python count function syntax: The count[] method will return an integer value, i.e., the count of the given element from the given string. It returns a 0 if the value is not found in the given string. The following example shows the working of count[] function on a string. Output:The syntax for PythonString Count[]
string.count[char or substring, start, end]
Parameters of Python Syntax
ReturnValue
Example 1: Count
Method on a String
str1 = "Hello World"
str_count1 = str1.count['o'] # counting the character “o” in the givenstring
print["The count of 'o' is", str_count1]
str_count2 = str1.count['o', 0,5]
print["The count of 'o' usingstart/end is", str_count2]
The count of 'o' is 2 The count of 'o' usingstart/end is 1
Example 2: Count occurrence of a character in a given string
The following example shows the occurrence of a character in a given string as well as in by using the start/end index.
str1 = "Welcome to Guru99 Tutorials!" str_count1 = str1.count['u'] # counting the character “u” in the given string print["The count of 'u' is", str_count1] str_count2 = str1.count['u', 6,15] print["The count of 'u' usingstart/end is", str_count2]
Output:
The count of 'u' is 3 The count of 'u' usingstart/end is 2
Example 3: Count occurrence of substring in a given string
Following example shows the occurrence of substring in a givenstring as well as usingstart/endindex.
str1 = "Welcome to Guru99 - Free Training Tutorials and Videos for IT Courses" str_count1 = str1.count['to'] # counting the substring “to” in the givenstring print["The count of 'to' is", str_count1] str_count2 = str1.count['to', 6,15] print["The count of 'to' usingstart/end is", str_count2]
Output:
The count of 'to' is 2 The count of 'to' usingstart/end is 1
Summary:
- The count[] is a built-in function in Python. It will return you the count of a given element in a list or a string.
- In the case of a string, the counting begins from the start of the string till the end. It is also possible to specify the start and end index from where you want the search to begin.
- The count[] method returns an integer value.