简体   繁体   中英

How to find most frequent list within a list - Python

For example, how would you find the most occurred list within a list of lists and appropriate counts. Counter from collections doesn't seem to accept lists as elements.

my_list = [[10, 2, 14], [1, 4, 6], [2, 3, 4], [10, 2, 14]]
# most frequent list: ([10, 2, 14], 2)

It does not accept mutable elements, but for simple lists you can convert it to tuples:

from collections import Counter

my_list = [[10, 2, 14], [1, 4, 6], [2, 3, 4], [10, 2, 14]]

Counter(tuple(i) for i in my_list).most_common()

you have to convert the most common element back to a list of course.

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