Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore case while doing most_common in Python's collections.Counter?

I'm trying to count the number of occurrences of an element in an iterable using most_common in the collections module.

>>> names = ['Ash', 'ash', 'Aish', 'aish', 'Juicy', 'juicy']
>>> Counter(names).most_common(3)
[('Juicy', 1), ('juicy', 1), ('ash', 1)]

But what I expect is,

[('juicy', 2), ('ash', 2), ('aish', 2)]

Is there a "pythonic" way/trick to incorporate the 'ignore-case' functionality , so that we can get the desired output.

like image 763
kmario23 Avatar asked Sep 14 '25 18:09

kmario23


1 Answers

How about mapping it to str.lower?

>>> Counter(map(str.lower, names)).most_common(3)
[('juicy', 2), ('aish', 2), ('ash', 2)]
like image 134
L3viathan Avatar answered Sep 16 '25 08:09

L3viathan