简体   繁体   中英

How to find most common element in a list of list?

I understand

a = max(set(lst), key=lst.count)

will derive most common element in a list

but how do you derive most common element in a list of list without using helper function?

For example

lst = [['1','2','3','4'],['1','1','1','1'],['1','2','3','4']]

The output should equal 1 .

When I try a = max(set(lst), key=lst.count)

it writes builtins.TypeError: unhashable type: 'list'

Can anyone please help me?

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, eg 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'

Just flatten your list of list , and use collections.Counter on it. Then use Counter.most_common() method to get a list of tuple of elements with their count of occurrence from highest to lowest: -

>>> lst = [['1','2','3','4'],['1','1','1','1'],['1','2','3','4']]
>>> flattened_list = [elem for sublist in lst for elem in sublist]  
>>> flattened_list
['1', '2', '3', '4', '1', '1', '1', '1', '1', '2', '3', '4']
>>>
>>> from collections import Counter
>>>
>>> counter = Counter(flattened_list)
>>> counter.most_common()
[('1', 6), ('3', 2), ('2', 2), ('4', 2)]
>>>
>>> counter.most_common(1)
('1', 6)

Or, you can use your method to get most common element from the flatten list.

>>> max(set(flattened_list), key=flattened_list.count)
'1'

You can also flatten your list like this: -

>>> sum(lst, [])
['1', '2', '3', '4', '1', '1', '1', '1', '1', '2', '3', '4']

So, as a one-liner, you can do it like this: -

>>> lst = [['1','2','3','4'],['1','1','1','1'],['1','2','3','4']]

>>> max(set(sum(lst, [])), key=sum(lst, []).count)
'1'

Of course, the last one creates two lists, with same content.

You have to flattern your list (with chain(*lst) ), then count entry of each element of your list with Counter(chain(*lst).most_common()) and sort the result.

from itertools import chain
from collections import Counter

lst = [['1','2','3','4'],['1','1','1','1'],['1','2','3','4']]
sorted(Counter(chain(*lst)).most_common())[0][0]

You could use Counter to find the most common element, and chain to iterate through the elements of the list of lists:

from collections import Counter
from itertools import chain

print Counter(val for val in chain.from_iterable(lst)).most_common(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM