Finding the most common element in a list of lists
Alternative solution without using the Counter
method from collections
:
def get_freq_tuple(data):
counts = {}
for pairs in data:
for pair in pairs:
counts[pair] = counts.get(pair, 0) + 1
return [pair for pair in counts if counts[pair] == max(counts.values())]
if __name__ == "__main__":
pairs = [[(0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (3, 2), (3, 1), (2, 1),
(3, 1), (3, 2), (3, 3), (3, 2), (2, 2)],
[(2, 2), (2, 1)],
[(1, 1), (1, 2), (2, 2), (2, 1)]]
print(get_freq_tuple(pairs))
Output:
[(2, 2)]
Explanation:
- Count the occurrences of each tuple and store them in a dictionary. The key of the dictionary is the tuple and the value is the occurrence.
- Filter the tuples in the dictionary by maximum occurrences of the tuples.
Disclaimer:
- Using
Counter
method fromcollections
is much more efficient.
References:
- Python dictionary
Find the most common element in list of lists
Apply Counter().update()
option on the elements of your list,
Based on suggestion from @BlueSheepToken
from collections import Counter
words = [['a','b','a','a'],['c','c','c','d','d','d']]
counter = Counter(words[0])
for i in words[1:]:
counter.update(i)
counter.most_common()
output:
[('a', 3), ('c', 3), ('d', 3), ('b', 1)]
How to find all of the most common elements in a python list (order alphabetically in case of tie)?
Here is a possible solution (as discussed in the comments):
from collections import Counter
lst = # your list of characters
c = Counter(lst) # O(n)
largest = max(counts.values()) # O(n)
largest_with_ties = [k for k, v in counts.items() if v == largest] # O(n)
result = sorted(largest_with_ties)
Now, what's the complexity of sorted(largest_with_ties)
? One could say that it's O(nlogn) (because there could be n/2 ties). However, the number of characters in largest_with_ties
cannot be as large as the number of elements in lst
. And that's because there is a much smaller number of characters compared to the possible number of ints. In other words, lst
could potentially contain 10^20 numbers (just an example). But largest_with_ties
can only contain different characters, and the number of characters that can be represented (for example) with UTF8 is limited to more or less 10^6. Therefore, technically the complexity of this last operation is O(1). In general, we could say that it's O(nlogn) but with an upper limit of O(10^6log10^6).
How to find most common element in a list of list?
There are many ways, but I wanted to let you know that there are some nice tools for that kind of things in the standard modules, e.g. collections.Counter
:
In [1]: lst = [['1','2','3','4'],['1','1','1','1'],['1','2','3','4']]
In [2]: from collections import Counter
In [3]: from operator import itemgetter
In [4]: max((Counter(l).most_common(1)[0] for l in lst), key=itemgetter(1))[0]
Out[4]: '1'
Or, you could (kinda) employ your current solution for each of the sublists:
In [5]: max(((max(set(l), key=l.count), l) for l in lst),
...: key=lambda x: x[1].count(x[0]))[0]
Out[5]: '1'
Finding common elements from lists in list
You need to convert the first list to a set so you can use the intersection()
method.
Use a loop rather than hard-coding all the indexes of the list elements.
def common_elements(lists):
if len(lists) == 0:
return []
common = set(lists[0])
for l in lists[1:]:
common = common.intersection(l)
return list(common)
How to find the N most common occurring element in a list?
Any particular reason why you would want to use networkx
?
You can achieve this simply with collections.Counter
and itertools.chain
:
from collections import Counter
from itertools import chain
l = [('a','b'), ('a','c'), ('b','e'), ('a','d')]
Counter(chain.from_iterable(l)).most_common(2)
NB. Here for the top 2
Output: [('a', 3), ('b', 2)]
To only get the keys in decreasing order of frequency:
c = Counter(chain.from_iterable(l))
list(dict(c.most_common(2)))
Output: ['a', 'b']
How to find several most frequent elements in a list
Here is a solution that works for any kind of data, not only for positive integers in a range known beforehand.
We count using a collections.Counter
, extract the maximum count which is the count of the most_common number, then make a list of the numbers who have the same count:
from collections import Counter
numbers = [7, 1, 7, 9, 2, 9, 7, 3, 0, 9]
counts = Counter(numbers)
max_count = counts.most_common(1)[0][1]
out = [value for value, count in counts.most_common() if count == max_count]
print(out)
# [7, 9]
Related Topics
How to Set Proxy Authentication (User & Password) Using Python + Selenium
Print a List of Space-Separated Elements
Pandas: Calculate the Percentage Between Two Rows and Add the Value as a Column
Find All CSV Files in a Directory Using Python
How to Pass a .Txt File to a Function in Python
How Does the Code Prints 1 2 6 24 as Output and Not 24 6 2 1
How to Remove Commas and Dots of Individual Word in Two Dimensional List
How to Expand Input Buffer Size of Pyserial
How to Get String Objects Instead of Unicode from Json
Executing Multiple Functions Simultaneously
How to Pivot on Multiple Columns in Spark SQL
Replace Only Exactly Matching String Python Ndarry
Webscraping Financial Data from Morningstar
Pyqt: Getting Widgets to Resize Automatically in a Qdialog
How to Read Pdf Files One by One from a Folder in Python