【问题标题】:How to find the total number of the same dictionary value? [duplicate]如何找到相同字典值的总数? [复制]
【发布时间】:2020-11-13 02:12:10
【问题描述】:

查找字典变量中每个值的总数的最佳方法是什么?

这是我的字典变量。

my_dict = {
        'mike':'football',
        'jack':'basketball',
        'tom':'basketball',
        'keanu':'basketball',
        'jason':'football'
}

这就是我想要的输出。

Total number of each value

football : 2
basketball : 3 

【问题讨论】:

标签: python


【解决方案1】:

你可以使用collections.Counter:

from collections import Counter

my_dict = {
        'mike':'football',
        'jack':'basketball',
        'tom':'basketball',
        'keanu':'basketball',
        'jason':'football'
}
print(Counter(my_dict.values()))

结果:

Counter({'basketball': 3, 'football': 2})

【讨论】:

    猜你喜欢
    • 2019-04-05
    • 2019-02-01
    • 2015-03-28
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    相关资源
    最近更新 更多