简体   繁体   English

Python将字典值转换为列出和追加项目

[英]Python convert dictionary values to list and append items

I have a dictionary of dictionaries named dictdict and it looks like: 我有一个名为dictdict的词典字典,它看起来像:

dictdict = { 'key1': {'subkey1': 4, 'subkey2': 7}, 
             'key2': {'subkey1': 6, 'subkey2': 8} }

...and so on. ...等等。 dictdict has a few dozen keys and for each key a few dozen subkeys, with each subkey corresponding to a single integer value. dictdict有几十个键,每个键有几十dictdict键,每个子键对应一个整数值。

I want to append a list onto each individual value keeping the value that is already there as the first item in my new list of values. 我想在每个单独的值上附加一个列表,保留已存在的值作为新值列表中的第一项。 So the desired result would have the same dictionary of dictionaries structure as before, except instead of the subkeys corresponding to a single integer, they would correspond to a list containing integers and strings (so a list that contains my original value as the first item followed by various integers and strings). 因此,期望的结果将具有与之前相同的字典结构字典,除了代替对应于单个整数的子键,它们将对应于包含整数和字符串的列表(因此包含我的原始值作为第一个项目的列表通过各种整数和字符串)。

I want to end up with something that looks like: 我想最终看起来像:

dictdict = { 'key1': {'subkey1': [4, 'apple', 37], 
                      'subkey2': [7, 'orange', 19]}, 
             'key2': {'subkey1': [6, 'kiwi', 76], 
                      'subkey2': [8, 'banana', 29]} }

When I try this... 当我试试这个......

#convert each nested value into a one item list
for key in dictdict:
    for subkey in dictdict[key]:
        dictdict[key][subkey] = [dictdict[key][subkey]] 

#now that our values are lists, append some items to the list
dictdict["key1"]["subkey1"].append('a1', a2) 

...it almost works, as I can append a list for each value (hooray!) however the first item in each list (my original value) is now a one item list of its own (oops!). ...它几乎可以工作,因为我可以为每个值附加一个列表(万岁!)但是每个列表中的第一项(我的原始值)现在是它自己的一个项目列表(哎呀!)。

So I end up with 所以我最终得到了

dictdict[key1][subkey1]

corresponding to this 对应于此

[[4], 'a1', a2]

but I would like it to correspond to this 但我希望它符合这一点

[4, 'a1', a2]

Also, it probably goes w/out saying, but I am a newbie. 此外,它可能不会说出来,但我是一个新手。

>>> dictdict = { 'key1': {'subkey1': 4, 'subkey2': 7}, 'key2': {'subkey1': 6, 'subkey2': 8} }
>>> dictdict["key1"]["subkey1"]
4
>>> for v1 in dictdict.values():
...     v1.update((k,[v]) for k,v in v1.iteritems())
... 
>>> dictdict["key1"]["subkey1"] += ['a1', 'a2']
>>> dictdict["key1"]["subkey1"]
[4, 'a1', 'a2']
>>> 

You can use a dict comprehension, like so: 您可以使用dict理解,如下所示:

dictdict = {k:{kk:[vv] for (kk, vv) in v.iteritems()} for (k, v) in dictdict.iteritems()}

For example, if this was your initial dict: 例如,如果这是你的初始字典:

>>> dictdict
{'a': {'mm': 4}, 'b': {'qq': 6}}

Then the result would be: 那么结果将是:

>>> dictdict = {k:{kk:[vv] for (kk, vv) in v.iteritems()} for (k, v) in dictdict.iteritems()}
>>> dictdict
{'a': {'mm': [4]}, 'b': {'qq': [6]}}

This gets you to the point where you can append items to the nested list(s). 这使您可以将项目附加到嵌套列表。 You can do that with += or extend() if you are appending another list, or with append() if you are appending a single element at a time. 如果要追加另一个列表,可以使用+=extend()如果要一次追加单个元素,则可以使用append()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM