在 Python 循环中构建字典 - 列表和字典推导式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19121722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Build Dictionary in Python Loop - List and Dictionary Comprehensions
提问by Mike
I'm playing with some loops in python. I am quite familiar with using the "for" loop:
我在 python 中玩一些循环。我非常熟悉使用“for”循环:
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):
然后我最近在 Stack 上发现了一种很好的高效循环类型来构建一个列表(这种类型的循环有名字吗?我真的很想知道这样我可以更好地搜索它):
[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:
好吧,话虽如此,我想更进一步地使用最后一种类型的循环,并尝试使用相同类型的逻辑构建一个 python 字典:
{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?
我的第二个问题是,我可以使用这种类型的循环构建一个 python 字典还是我离基地很远?如果是这样,我将如何构建它?
采纳答案by lejlot
The short form is as follows (called dict comprehension, as analogy to the list comprehension, set comprehensionetc.):
简短形式如下(称为dict comprehension,类似于list comprehension、set comprehension等):
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:
所以一般来说,给定一些_container
具有某种元素的函数和一个函数_value
,该函数对于给定元素返回您要添加到字典中此键的值:
{ _key : _value(_key) for _key in _container }
回答by Carsten
You can do it like this:
你可以这样做:
x = dict((row.SITE_NAME, row.LOOKUP_TABLE) for row in cursor)
回答by Wayne Werner
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.
或者,如果你有一个足够新的 Python 版本,有一种叫做字典推导的东西——它的工作原理类似于列表推导,但生成的是一个字典。
x = {row.SITE_NAME : row.LOOKUP_TABLE for row in cursor}