在Python循环中构建字典 - 列表和字典理解

时间:2023-02-12 09:56:48

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):

然后我最近发现了一个很好的有效类型的循环,在这里,在堆栈上,建立一个列表(这个类型的循环有一个名称吗?我真的很想知道所以我可以更好地搜索它):

[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字典,还是我离开基地?如果是这样,我将如何构建它?

3 个解决方案

#1


43  

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:

所以通常给一些带有某种元素的_container和一个函数_value,对于给定的元素,它返回你要在字典中添加到这个键的值:

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

#2


8  

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}

#3


5  

You can do it like this:

你可以这样做:

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

#1


43  

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:

所以通常给一些带有某种元素的_container和一个函数_value,对于给定的元素,它返回你要在字典中添加到这个键的值:

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

#2


8  

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}

#3


5  

You can do it like this:

你可以这样做:

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