How to construct nested dictionary comprehension in Python with correct ordering?

两盒软妹~` 提交于 2020-06-25 04:36:30

问题


I was trying to shorten the code for this problem when I encountered the problem.

Basically, I was trying a nested dictionary comprehension & was unsuccessful in the attempt. Here is what I tried.

dict2 = {key:value for key, value in line.split(":")
                   for line in ["1:One", "2:Two", "4:Four"]}
print dict2

When I run this, it gives me

NameError: name 'line' is not defined

And, when I reverse the for statements like this

dict2 = {key:value for line in ["1:One", "2:Two", "4:Four"]
                   for key, value in line.split(":")}
print dict2

It results in

ValueError: need more than 1 value to unpack

I need help on the nesting structure of dictionary (or list) comprehension. An example would be help a lot.


回答1:


Note that there is a better way of doing this without a dict comprehension; see below. I’ll first address the issues with your approach.

You need to use nesting order in comprehensions. List your loops in the same order they would be in when nesting a regular loop.

The line.split() expression returns a sequence of two items, but each of those items is not a tuple of a key and a value; instead there is only ever one element being iterated over. Wrap the split in a tuple so you have a 'sequence' of (key, value) items being yielded to assign the two results to two items:

dict2 = {key:value for line in ["1:One", "2:Two", "4:Four"]
                   for key, value in (line.split(":"),)}

This is the equivalent of:

dict2 = {}
for line in ["1:One", "2:Two", "4:Four"]:
    for key, value in (line.split(":"),):
        dict2[key] = value

where the nested loop is only needed because you cannot do:

dict2 = {}
for line in ["1:One", "2:Two", "4:Four"]:
    key, value = line.split(":")
    dict2[key] = value

However, in this case, instead of a dictionary comprehension, you should use the dict() constructor. It wants two-element sequences, simplifying the whole operation:

dict2 = dict(line.split(":") for line in ["1:One", "2:Two", "4:Four"])



回答2:


You can do this a lot simpler with dict and a generator expression:

>>> lst = ["1:One", "2:Two", "4:Four"]
>>> dict(x.split(":") for x in lst)
{'4': 'Four', '1': 'One', '2': 'Two'}
>>>


来源:https://stackoverflow.com/questions/20446526/how-to-construct-nested-dictionary-comprehension-in-python-with-correct-ordering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!