在字典中查找键的最重复值

时间:2015-11-21 05:34:31

标签: python python-2.7

我有以下字典:

s_final = {
    a:[noun,adj,noun,noun,noun],
    b:[verb,verb,verb,noun]
}

我想检索最重复的键值,以便我的最终输出如下:

s_final={a:[noun], b:[verb]}

1 个答案:

答案 0 :(得分:3)

使用Counter

>>> from collections import Counter
>>> s_final={'a':['noun','adj','noun','noun','noun'],'b':['verb','verb','verb','noun']}
>>> {i:[Counter(j).most_common(1)[0][0]] for i,j in s_final.items()}
{'a': ['noun'], 'b': ['verb']}