简体   繁体   中英

how to find most frequent values in a list?

I want to find frequent values in a list using collections.Counter . Here is an example list:

from collections import Counter

lastNodes = ['138885662','192562407','192562407','138885662','121332964','185592680','144024400','144024400','144024400','138885662']
c = Counter(lastNodes)

output of print(c) :

Counter({'121332964': 1,
         '138885662': 3,
         '144024400': 3,
         '185592680': 1,
         '192562407': 2})

I used c.most_common(1) to get the most frequent value, which output [('138885662', 3)] .

but I want to get values which have 3 times repeated in the list. desired output:

[('138885662', 3), ('144024400', 3)]

and what if I want those which have 3 and 2 repeated values:

[('138885662', 3), ('144024400', 3), ('192562407', 2)]

of course this is an example list. I have an algorithm which generates dynamic lists. so I don't know how many most frequent values exist in every list

With itertools:

from itertools import chain, islice, groupby
from operator import itemgetter

n = 2

# group the items having same count together
grouped = (list(group) for _, group in groupby(c.most_common(), key=itemgetter(1)))

# slice first `n` of them
top_n = islice(grouped, n)

# flatten
result = list(chain.from_iterable(top_n))

where itemgetter(1) helps grouping items of counter according to their frequencies (in tuples, 0th entry is item itself, 1st entry is its count so we use 1; also noting that groupby expects a sorted data which most_common provides).

sample runs:

# for n = 1
[("138885662", 3), ("144024400", 3)]

# for n = 2
[("138885662", 3), ("144024400", 3), ("192562407", 2)]

# for n = 3
[("138885662", 3), ("144024400", 3), ("192562407", 2), ("121332964", 1), ("185592680", 1)]

Just take 2 of them in most_common :

>>> c.most_common(2)
[('138885662', 3), ('144024400', 3)]

what you can do is loop through the list and use the count function and if the number of occurences of that element is 3 append it to another list and

lst = []
for i in lastNodes:
    if lastNodes.count(i) == 3:
        lst.append(i)```

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