简体   繁体   中英

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.

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

In most cases, list comprehension is faster than the equivalent for -loop and clearly a more compact way to express an algorithm. You can have nested list comprehensions too (just as you can have nested for -loops), but they can quickly become hard to comprehend :)

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. It's well worth learning about all of these.

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

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

This is a feature of Python called 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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