简体   繁体   English

在 Python 中将列表推导式转换为 For 循环

[英]Converting List Comprehensions to For Loops in Python

I understand the importance of list comprehensions, but do not understand their inner-workings, thus am not able to understand them in simpler terms such as I would a for loop.我了解列表推导式的重要性,但不了解它们的内部工作原理,因此无法用更简单的术语(例如 for 循环)来理解它们。 For example, how could I change this to a for loop:例如,我如何将其更改为 for 循环:

li = [row[index] for row in outer_list]

In this example I have a list of lists, for my purposes a matrix, called outer_list which is a list of lists.在这个例子中,我有一个列表列表,为了我的目的,我有一个矩阵,称为outer_list ,它是一个列表列表。 The index value is the column number the outer iteration is at, from 0 to some number n.索引值是外部迭代所在的列号,从 0 到某个数字 n。 The list comprehension above returns the index 'th column in the matrix, and assigns it to li as a list.上面的列表推导式返回矩阵中索引的第 ' 列,并将其作为列表分配给li How can I create a for loop that creates a list in this manner?如何创建以这种方式创建列表的 for 循环?

It's just a shorter way of expressing a list .这只是表达list一种更短的方式。

li = [row[index] for row in outer_list]

is equivalent to:相当于:

li = []
for row in outer_list:
    li.append(row[index])

Once you get used to the syntax, it becomes a tidy way of creating list s (and other iterables).一旦习惯了语法,它就会成为创建list (和其他可迭代对象)的一种整洁方式。

Question:题:

How could I change this to a for loop?我怎么能把它改成 for 循环?

li = [row[index] for row in outer_list]

Answer:回答:

li = []
for row in outer_list:
   li.append(row[index])

Assuming the outer iteration is for the index, this code can be effectively re-written as :假设外部迭代是针对索引的,这段代码可以有效地重写为:

for row in outer_list:
    li.append(row[index])

Here you are iterating in the lists inside the outer_list .在这里,您正在对outer_list内的列表进行迭代。 So, the first iteration would add the first list inside outer_list to li , then the second one is appended and so on.因此,第一次迭代会将outer_list的第一个列表添加到li ,然后附加第二个列表,依此类推。

Your question seems to imply that this snippet is already inside of a for loop, like this:您的问题似乎暗示此代码段已经在 for 循环内,如下所示:

outer_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for index in xrange(3):
    li = [row[index] for row in outer_list]
    print li

which would output:这将输出:

[1, 4, 7]
[2, 5, 8]
[3, 6, 9]

You could convert this to use only conventional for loops by nesting an inner loop inside of your outer loop:您可以通过在外循环内嵌套一个内循环来将其转换为仅使用传统的for循环:

outer_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for index in xrange(3):
    li = []                    # initial empty list
    for row in outer_list:     # our inner loop
        li.append(row[index])  # Build the list "li" iteratively
    print li

If you are treating this nested list as a matrix, then what you describe is essentially taking the transpose of the matrix.如果您将此嵌套列表视为矩阵,那么您所描述的实际上是对矩阵进行转置。 You can do this entirely via list comprehensions as well:你也可以完全通过列表推导来做到这一点:

outer_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

transpose = [[row[i] for row in outer_list] for i in range(len(outer_list[0]))]

print transpose

which yields:产生:

[[1, 4, 7],
[2, 5, 8],
[3, 6, 9]]

As shown in the official Python tutorial here .如图Python的官方教程这里

I've included the nested list comprehension example by way of completeness but a word to the wise: using nested list comprehensions generally makes code less comprehensible, not more.出于完整性的考虑,我已经包含了嵌套列表理解示例,但明智的说法是:使用嵌套列表理解通常会使代码更难理解,而不是更多。 Just because you can doesn't mean you should.仅仅因为你可以并不意味着你应该。

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

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