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

前端 未结 4 1812
梦毁少年i
梦毁少年i 2020-12-19 23:13

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

相关标签:
4条回答
  • 2020-12-19 23:30

    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)
    
    0 讨论(0)
  • 2020-12-19 23:32

    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]
    
    0 讨论(0)
  • 2020-12-19 23:34

    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.

    0 讨论(0)
  • 2020-12-19 23:37

    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'
    
    0 讨论(0)
提交回复
热议问题