如何在Python的集合中执行most_common时忽略大小写。计数器?(How to ignore case while doing most_common in Python's collections.Counter?)
我正在尝试使用collections模块中的most_common迭代中元素的出现次数。
>>> names = ['Ash', 'ash', 'Aish', 'aish', 'Juicy', 'juicy'] >>> Counter(names).most_common(3) [('Juicy', 1), ('juicy', 1), ('ash', 1)]但我的期望是,
[('juicy', 2), ('ash', 2), ('aish', 2)]是否存在“pythonic”方式/技巧来合并'ignore-case'功能,以便我们可以获得所需的输出。
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.
最满意答案
把它映射到str.lower怎么str.lower ?
>>> Counter(map(str.lower, names)).most_common(3) [('juicy', 2), ('aish', 2), ('ash', 2)]How about mapping it to str.lower?
>>> Counter(map(str.lower, names)).most_common(3) [('juicy', 2), ('aish', 2), ('ash', 2)]更多推荐
发布评论