Build Dictionary in Python Loop - List and Dictionary Comprehensions

人走茶凉 提交于 2020-12-24 02:51:29

问题


I'm playing with some loops in python. I am quite familiar with using the "for" loop:

for x in y:
    do something

You can also create a simple list using a loop:

i = []
for x in y:
   i.append(x)

and then I recently discovered a nice efficient type of loop, here on Stack, to build a list (is there a name for this type of loop? I'd really like to know so I can search on it a little better):

[x.name for x in y]

Ok, that being said, I wanted to go further with the last type of loop and I tried to build a python dictionary using the same type of logic:

{x[row.SITE_NAME] = row.LOOKUP_TABLE for row in cursor}

instead of using:

x = {}
for row in cursor:
   x[row.SITE_NAME] = row.LOOKUP_TABLE

I get an error message on the equal sign telling me it's an invalid syntax. I believe in this case, it's basically telling me that equal sign is a conditional clause (==), not a declaration of a variable.

My second question is, can I build a python dictionary using this type of loop or am I way off base? If so, how would I structure it?


回答1:


The short form is as follows (called dict comprehension, as analogy to the list comprehension, set comprehension etc.):

x = { row.SITE_NAME : row.LOOKUP_TABLE for row in cursor }

so in general given some _container with some kind of elements and a function _value which for a given element returns the value that you want to add to this key in the dictionary:

{ _key : _value(_key) for _key in _container }



回答2:


What you're using is called a list comprehension. They're pretty awesome ;)

They have a cousin called a generator expression that works like a list comprehension but instead of building the list all at once, they generate one item at a time. Hence the name generator. You can even build functions that are generators - there are plenty of questions and sites to cover that info, though.

You can do one of two things:

x = dict(((row.SITE_NAME, row.LOOKUP_TABLE) for row in cursor))

Or, if you have a sufficiently new version of Python, there is something called a dictionary comprehension - which works like a list comprehension, but produces a dictionary instead.

x = {row.SITE_NAME : row.LOOKUP_TABLE for row in cursor}



回答3:


You can do it like this:

x = dict((row.SITE_NAME, row.LOOKUP_TABLE) for row in cursor)


来源:https://stackoverflow.com/questions/19121722/build-dictionary-in-python-loop-list-and-dictionary-comprehensions

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