python - 如何在 Python collections.Counter 中执行 most_common 时忽略大小写?

标签 python string python-3.x list python-collections

我正在尝试使用 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' 功能,以便我们可以获得所需的输出。

最佳答案

如何将它映射到 str.lower

>>> Counter(map(str.lower, names)).most_common(3)
[('juicy', 2), ('aish', 2), ('ash', 2)]

关于python - 如何在 Python collections.Counter 中执行 most_common 时忽略大小写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35184306/

相关文章:

python - 生成具有正确缩进的 XML 文件

python - 重复数组的不同元素不同的次数

php - PHP 中单引号和双引号字符串有什么区别?

string - Rabin karp字符串匹配算法复杂度

python - 在 python 中将原始数据类型名称作为函数参数传递

python - Python中Redis连接池的正确使用方式

Python 3.0导入Excel文件到Access文件

python - pip安装mysql报错

C++ 错误 C4430 int 假定

python - 作为 Snakefile 输出的函数或字典理解