python - 使用 Python 计算列表中最常见的项目

标签 python list collections

我试图显示列表中 n 个最常见的项目,但收到错误:TypeError: unhashable type: 'list'

import collections

test = [[u'the\xa0official', u'MySQL'], [u'MySQL', u'repos'], [u'repos', u'for'], [u'for', u'Linux'], [u'Linux', u'a'], [u'a', u'little'], [u'little', u'over'], [u'over', u'a'], [u'a', u'year'], [u'year', u'ago,'], [u'ago,', u'the'], [u'the', u'offering'], [u'offering', u'has'], [u'has', u'grown'], [u'grown', u'steadily.\xa0Starting'], [u'steadily.\xa0Starting', u'off'], [u'off', u'with'], [u'with', u'support'], [u'support', u'for'], [u'for', u'the'], [u'the', u'Yum'], [u'Yum', u'based'], [u'based', u'family'], [u'family', u'of\xa0Red'], [u'of\xa0Red', u'Hat/Fedora/Oracle'], [u'Hat/Fedora/Oracle', u'Linux,'], [u'Linux,', u'we'], [u'we', u'added'], [u'added', u'Apt'], [u'Apt', u'repos'], [u'repos', u'for'], [u'for', u'Debian'], [u'Debian', u'and'], [u'and', u'Ubuntu'], [u'Ubuntu', u'in'], [u'in', u'late'], [u'late', u'spring,'], [u'spring,', u'and'], [u'and', u'throughout'], [u'throughout', u'all']]

print test[0]
print type(test)

print collections.Counter(test).most_common(3)

最佳答案

>>> print collections.Counter(map(tuple,test)).most_common(3)
[((u'repos', u'for'), 2), ((u'and', u'throughout'), 1), ((u'based', u'family'), 1)]

关于python - 使用 Python 计算列表中最常见的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27567542/

相关文章:

python - 在具有相互依赖值的矩阵中向量化计算

python - 在 debian 上打包使用 perl 和 python 的应用程序

python - 我正确使用pickle吗?-Python

java - 使用堆栈(LinkedList)将中缀转换为前缀

c# collection.RemoveAll(collection.OfType<type>());

java - Hibernate 合并不适用于属性作为集合

python - Sobel 算子 - Opencv Python

python - 使用键作为索引从字典列表创建 DataFrame

vba - 如何在 vba 中创建循环迭代 block 中的列表?

c# - 如何基于多个xml属性存储和查找数据?