获取value_counts()作为字典的值

时间:2019-10-20 04:06:02

标签: python dictionary counter

我非常熟悉如何从pd.Series返回value_counts。但是,如何从字典的值中获取值计数?

假设我在字典L中具有以下元组:

L = {1: (13600, 14797),
     2: (14700, 14700),
     3: (14700, 10400),
     4: (14600, 17200),
     5: (13600, 14797),
     6: (14600, 17200),
     7: (14700, 10400),
     8: (14700, 10400),
     9: (12800, 14770)}

如何从value_counts获取L,如下所示:

(14700, 10400) 3
(13600, 14797) 2
(14600, 17200) 2
(14700, 14700) 1
(12800, 14770) 1

这是我到目前为止所拥有的。但是,我认为字典键1-9妨碍了操作,因为出现错误list object is not callable.

list = [(k, v) for k, v in L.items()] 
S = set(L)
F = {}
for i in list(S):
    F[i] = list.count(i)

4 个答案:

答案 0 :(得分:5)

使用标准库中的collections.Counter

Counter(L.values())

答案 1 :(得分:2)

也许使用from collections import Counter是个好主意吗?

from collections import Counter 
dict(Counter([j for i,j in L.items()]))

答案 2 :(得分:2)

您可以尝试以下方法:

@client.command()
async def test(ctx):
    print("Username is = " + ctx.message.author)
    print(ctx.message.author + "'s avatar url is = " )

答案 3 :(得分:2)

您提到pandas.Series.value_counts(),为什么不尝试

pd.Series(L).value_counts()

给出:

(14700, 10400)    3
(14600, 17200)    2
(13600, 14797)    2
(12800, 14770)    1
(14700, 14700)    1
dtype: int64