python字典中相同值找键,字典Python中重复键值的列表

Apologies in advance if this question has already been explored here - I looked at different answers here but couldn't find what I need.

My goal is to create a dictionary like this -- {'a':[10, 9, 10, 10], 'b':[10, 9, 1, 0], 'c':[0, 5, 0, 1], and so on}

What I have is multiple dictionaries with duplicate keys (same keys in every other dictionary), something like this

{'a':10, 'b': 0, 'c': 2}

{'a':7, 'b': 4, 'c': 4}

{'a':4, 'b': 5, 'c': 3}

I have no way of knowing the number of such dictionaries, or if there are keys continuing up to 'f', or a 'g' in them but I know that the keys are duplicated. I've tried defaultdict but what I get is--

defaultdict(, {'a': [10]})

defaultdict(, {'a': [10], 'b': [3]})

defaultdict(, {'a': [10], 'b': [3], 'c': [0]})

and then the same thing for the next dictionary --

defaultdict(, {'a': [4]})

defaultdict(, {'a': [4], 'b': [5]})

defaultdict(, {'a': [4], 'b': [5], 'c': [1]})

The code I have for the above output is --

d = collections.defaultdict(list)

for k, v in z.iteritems():

d[k].append(v)

c = d.items()

print d

If I do a print c instead (to print the d.items()) I get --

[('a', [10])]

[('a', [10]), ('b', [3])]

[('a', [10]), ('c', [0]), ('b', [3])]

which is again repeated for each dictionary. How do I get 1 dict holding all the keys, values --

{'a':[10,0,..], 'b':[4, 3, 4,..], etc.} ?

I should also add that the dicts I have are the result of a for loop and not stored individually in a unique variable.

解决方案

If I understand correctly your attetion, you are trying to merge various dictionaries. One way using built-ins (I am sure that soon someone will give you a numpy and collections answer) could look like this:

ds = [

{'a':10, 'b': 0, 'c': 2},

{'a':7, 'b': 4, 'c': 4},

{'a':4, 'b': 5, 'c': 3} ]

merged = {}

for d in ds:

for k, v in d.items ():

if k not in merged: merged [k] = []

merged [k].append (v)

print (merged)

(Quite verbose for clarity)

EDIT:

After having read your comment stating "The result I want is a list of values/key.", you can use this on the resulting merged dictionary:

print ( [ (v, k) for k, v in merged.items () ] )

This yields:

[([10, 7, 4], 'a'), ([2, 4, 3], 'c'), ([0, 4, 5], 'b')]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值