简体   繁体   English

这个“[... for .. in ..]”如何在Python中运行?

[英]How does this “[.. for .. in ..]” work in Python?

Tell me please, how does it work exactly? 请告诉我,它是如何工作的? Why does each iteration result write to array? 为什么每个迭代结果写入数组?

list_of_strings = [a.rstrip('\n') for a in list_of_strings]

This list comprehension : 这个列表理解

list_of_strings = [a.rstrip('\n') for a in list_of_strings]

is equivalent to this more generic code: 相当于这个更通用的代码:

temp_list=[]
for a in list_of_strings:
   temp_list.append(a.rstrip('\n'))

list_of_strings=temp_list

In most cases, a list comprehension is faster and easier to read. 在大多数情况下,列表理解更快更容易阅读。 Learn it and use it if you want to write nontrivial Python. 学习它并使用它,如果你想编写非平凡的Python。

This takes a list of strings (stored in variable list_of_strings ), iterates through it assigning each string temporarily to variable a each time through and strips the \\n character from each string. 这将获取一个字符串列表(存储在变量list_of_strings ),迭代它每次将每个字符串临时分配给变量a并从每个字符串中删除\\n字符。 The result is a list of strings that is assigned back to the same variable. 结果是分配回同一变量的字符串列表

This is using List Comprehension . 这是使用列表理解 Generally most for-loops and list operations involving append() can be converted to equivalent list comprehensions. 通常, 涉及append()的大多数for循环和列表操作可以转换为等效的列表推导。 Here's a simple example demonstrating this: 这是一个简单的例子来证明这一点:

   new_list = []
   for i in range(10):
       new_list.append(i**2)

could be rewritten simply as 可以简单地重写为

   new_list = [i**2 for i in range(10)]

You may find it instructive to take your list comprehension and rewrite it with a for -loop to test your understanding (both should produce identical results). 您可能会发现使用列表理解并使用for -loop重写它来测试您的理解(两者都应该产生相同的结果) for有益的。

In most cases, list comprehension is faster than the equivalent for -loop and clearly a more compact way to express an algorithm. 在大多数情况下,列表理解比-loop的等效物更快for并且显然是表达算法的更紧凑的方式。 You can have nested list comprehensions too (just as you can have nested for -loops), but they can quickly become hard to comprehend :) 您也可以使用嵌套列表推导(就像您可以嵌套for -loops一样),但它们很快就会变得难以理解:)

Also, while not shown in the code above, you can filter values in the list comprehension, ie, including them (or not) in the resulting list depending on whether they meet some criteria. 此外,虽然上面的代码中没有显示,但您可以过滤列表推导中的值,即在结果列表中包含它们(或不包括它们),具体取决于它们是否符合某些条件。

As an aside, Python also provides similar set comprehensions and generator expressions. 另外,Python还提供了类似的集合理解和生成器表达式。 It's well worth learning about all of these. 所有这些都值得学习。

请阅读Python中的列表推导简介,以便更好地理解Python的列表推导语法。

每个迭代结果都不会写入列表,而是在右侧创建一个全新的列表,然后分配给=符号左侧的变量list_of_strings。

This is a feature of Python called list comprehensions. 这是Python的一个名为list comprehensions的特性。 Basically, what you write in the brackets is shorthand for 'do this loop and then create a list of the results'. 基本上,您在括号中写的是'执行此循环然后创建结果列表'的简写。

List Comprehensions have the following generic form 列表理解具有以下通用形式

result = [ x for x in list]

the equivalent procedural code for this list comprehension is: 该列表理解的等效程序代码是:

for x in list:
    x


The next step in creating a useful list comprehensions is calling or applying a function to the first x after the bracket. 创建有用列表推导的下一步是调用或将函数应用于括号后的第一个x。

result = [function(x) for x in list]
result = [x.function(arg) for x in list]

list_of_strings = [a.rstrip('\n') for a in list_of_strings]

result = [x*2 for x in list_of_numbers]

the equivalent procedural code for this list comprehension is: 该列表理解的等效程序代码是:

for x in list:
    function(x)

OR 要么

for x in list:
    x.function()


To add more power to your list comprehensions you can add conditionals like this: 要为列表推导添加更多功能,您可以添加以下条件:

result = [function(x) for x in list if x == True]

list_of_strings = [a.rstrip('\n') for a in list_of_strings if '\n' in a]

result = [x*2 for x in [1,2,3,4,5,6,7] if x > 3]

List Comprehensions make great filter like functions and are often as fast as the hard c coded primitives like map and filter in cpython. 列表理解可以很好地过滤类似函数,并且通常与cpython中的map和filter等硬编码基元一样快。

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

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