字典Python中重复键值的列表 [英] List of values for duplicate keys in dictionary Python

查看:799
本文介绍了字典Python中重复键值的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这个问题已经在这里进行了探讨,我们提前道歉 - 我在这里查看了不同的答案,但找不到我需要的。



我的目标是创建这样的字典 - {'a':[10,9,10,10],'b':[10,9,1,0],'c':[0,5, 0,1]等等}



我所拥有的是重复键的多个字典(每个其他字典中的相同的键),一些东西像这样
{'a':10,'b':0,'c':2}
{' a':4,'c':4}
{'a':4,'b':5,'c' :3}



我无法知道这些字典的数量,或者如果钥匙持续到'f '或'g',但我知道这些键是重复的。我试过 defaultdict ,但我得到的是 -

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

然后下一个字典相同的东西 -

  defaultdict(< type'list'> {'a':[4]})
defaultdict(< type'list'> {'a' 4],'b':[5]})
defaultdict(< type'list'>,{'a':[4],'b':[5],'c' ]})

上面输出的代码是 -

  d = collections.defaultdict(list)
for k,v in z.iteritems():
d [k] .append v)
c = d.items()
打印d

如果我一个 print c (打印 d.items())我得到 -

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

每个字典再次重复。如何获得1个持有所有键的值,值 -

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

我还应该补充一点,我是一个for循环的结果,而不是单独存储一个唯一的变量。

解决方案

如果我正确理解你的想法,你正在尝试合并各种字典。一种使用内置功能的方法(我确定很快有人会给你一个 numpy 集合答案)可以看像这样:

  ds = [
{'a':10,'b':0,'c' :2},
{'a':7,'b':4,'c':4},
{'a':4,'b':5,'c' }]

merged = {}
d中的d:
为k,v在d.items()中:
如果k未合并:合并[ k] = []
合并[k] .append(v)

打印(合并)

(为了清楚起见,请详细填写)



编辑:
阅读完您的评论后,我想要的结果是值/键列表,您可以在结果合并的字典中使用:

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

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


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(<type 'list'>, {'a': [10]})
defaultdict(<type 'list'>, {'a': [10], 'b': [3]})
defaultdict(<type 'list'>, {'a': [10], 'b': [3], 'c': [0]})

and then the same thing for the next dictionary --

defaultdict(<type 'list'>, {'a': [4]})
defaultdict(<type 'list'>, {'a': [4], 'b': [5]})
defaultdict(<type 'list'>, {'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')]

这篇关于字典Python中重复键值的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆