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

前端 未结 4 1811
梦毁少年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)
    

提交回复
热议问题