Question

I find the Counter() function in python very useful, and it has a built-in Counter.most_common() function to return the keys in order of most to least common. But I haven't found a build_in function to filter out all keys that are unique, or over/under a threshold of appearances. This isn't too difficult to do with a dictionary comphrension, but seems like such a common function, I was wondering (a) why it's not built-in, and (b) did I miss it?

My method, given some data:

x = ['904692239', '904692239', '416618990', '965059531', '904692239', '48644135', '904692239', '386210409', '978527886', '102666625', '793573909', '826761606', '980035897', '837619792', '709804744', '703248895', '904692239', '843796438', '633621488', '374214079', '904692239', '218851385', '359342704', '793949601', '793949601', '216731638', '793949601', '721831814', '715338006', '466865724', '160977579', '971821714', '715338006', '612216206', '658467007', '67897613', '255688245', '457452213', '457452213', '984706137', '160977579', '160977579', '503944932', '261444687', '95794369', '286082560', '974609408', '457408015', '376428770', '636170296', '636170296', '636170296', '721831814']
from collections import Counter
y = Counter(x)
non_uniques = [k for k,v in Counter(x).items() if v > 1]
>>>non_uniques
['715338006', '457452213', '904692239', '160977579', '793949601', '636170296', '721831814']

Is this the most efficient way to do this? (take a list and filter out all non-unique occurrences in that list)

Was it helpful?

Solution

No, there is no built-in method, this is not usually a requirement.

You can use itertools.takewhile() to get all 'unique' keys:

from itertools import takewhile

for unique in takewhile(lambda pair: p[1] == 1, reversed(y.most_common())):

or for all non-unique:

for unique in takewhile(lambda pair: p[1] > 1, y.most_common()):

Both methods do require a full sort of the dictionary, so your own method, looping just once, is fine too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top