我是靠谱客的博主 苹果外套,最近开发中收集的这篇文章主要介绍python values count_Python计算列表字典中的多个值(Python count multiple values in dictionary of list)...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

I have been trying to count the values in a dictionary with respect to the key. However, I could not achieved desired result. I will demonstrate with more details below:

from collections import Counter

d = {'a': ['Adam','Adam','John'], 'b': ['John','John','Joel'], 'c': ['Adam','Adam','John}

# create a list of only the values you want to count,

# and pass to Counter()

c = Counter([values[1] for values in d.itervalues()])

print c

My output:

Counter({'Adam': 2, 'John': 1})

I want it to count everything in the list, not just first value of the list. Also, I want my result to be with respect to the key. I will show you my desired output below:

{'a': [{'Adam': 1, 'John': 2}, 'b':{'John': 2, 'Joel': 1}, 'c':{'Adam': 2, 'John': 1 }]}

Is it possible to get this desired output? Or anything close to it? I would like to welcome any suggestions or ideas that you have. Thank you.

最后

以上就是苹果外套为你收集整理的python values count_Python计算列表字典中的多个值(Python count multiple values in dictionary of list)...的全部内容,希望文章能够帮你解决python values count_Python计算列表字典中的多个值(Python count multiple values in dictionary of list)...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部