Find the item with maximum occurrences in a list
Here is a defaultdict
solution that will work with Python versions 2.5 and above:
from collections import defaultdict
L = [1,2,45,55,5,4,4,4,4,4,4,5456,56,6,7,67]
d = defaultdict(int)
for i in L:
d[i] += 1
result = max(d.iteritems(), key=lambda x: x[1])
print result
# (4, 6)
# The number 4 occurs 6 times
Note if L = [1, 2, 45, 55, 5, 4, 4, 4, 4, 4, 4, 5456, 7, 7, 7, 7, 7, 56, 6, 7, 67]
then there are six 4s and six 7s. However, the result will be (4, 6)
i.e. six 4s.
No. of occurrences of maximum item in a list
Use max
and list.count
:
max_element= max(L)
count= L.count(max_element)
print(count)
Python: Counting maximum occurences of an item in a list with ties allowed
Build a Counter
from the list and take the items that match the highest count using a list comprehension:
from collections import Counter
lst = [1,1,2,2]
c = Counter(lst)
maximums = [x for x in c if c[x] == c.most_common(1)[0][1]]
print(maximums)
# [1, 2]
The Counter
approach take counts in one pass (O(n)) whereas the list.count
approach has O(n^2) time complexity as it passes through the list with each call.
Retrieve elements with max occurences with a DRAW(tie) in the max occurrence
You could do something like this:
counts = df[0].value_counts()
counts = counts[counts == counts.max()]
Output:
>>> counts
s 3
d 3
Name: 0, dtype: int64
>>> counts['s']
3
>>> counts['d']
3
Maximum Number of Occurrences of Any Item in Counter object / list
Since Pranav types faster than me, here is another potential answer to your problem:
ans = max(Counter(x for row in grid for x in row if x!=0).values())
I thought about irregular nested lists and this is what can do it:
unfurl=lambda x: sum(map(unfurl,x),[]) if isinstance(x,list) or isinstance(x, tuple) else [x]
ans = max(Counter(unfurl(grid)).values())
I am trying to find the maximum occurrences of a string in a list using Python [closed]
Instead of writing your own algorithm, you could simply use the builtin Counter
from collections
package. This class is basically a dictionary with items as keys and frequencies as values:
from collections import Counter
string = 'mary, bob, mary, bob, alex, julie'
names = [name.strip() for name in string.split(', ')]
frequencies = Counter(names)
max_frequency = max(frequencies.values())
max_frequency_names = [name
for name, frequency in frequencies.items()
if frequency == max_frequency]
print(max_frequency_names)
Related Topics
How to Update a Label Inside While Loop in Tkinter
How to Test If an Enum Member With a Certain Name Exists
How to Find the Average Colour of an Image in Python With Opencv
Python: Pandas Pd.Read_Excel Giving Importerror: Install Xlrd >= 0.9.0 for Excel Support
How to Remove the Double Quote When the Value Is Empty in Spark
Efficient Way to Unnest (Explode) Multiple List Columns in a Pandas Dataframe
How to Split Image into Multiple Pieces in Python
How to Remove Text Within Parentheses With a Regex
How to Detect and Remove Outliers from Each Column of Pandas Dataframe At One Go
Filtering a Pyspark Dataframe Using Isin by Exclusion
Print the Student Name and the Score of Student in Python3
Add Numpy Array as Column to Pandas Data Frame
Valueerror: Time Data Does Not Match Format When Parsing a Date
How-To Run Tensorflow on Multiple Core and Threads
Python Pandas .Isnull() Does Not Work on Nat in Object Dtype