单行嵌套 For 循环 [英] Single Line Nested For Loops

查看:27
本文介绍了单行嵌套 For 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用python编写了这个转置矩阵的函数:

def transpose(m):高度 = len(m)宽度 = len(m[0])返回 [ [ m[i][j] for i in range(0, height) ] for j in range(0, width) ]

在这个过程中我意识到我并不完全理解单行嵌套的 for 循环是如何执行的.请回答以下问题帮助我理解:

  1. 这个 for 循环的执行顺序是什么?
  2. 如果我有一个三重嵌套的 for 循环,它会按什么顺序执行?
  3. 什么是等价的非嵌套 for 循环?

鉴于,

[ function(i,j) for i,j in object ]

  1. 对象必须是什么类型才能使用这个 for 循环结构?
  2. 将 i 和 j 分配给 object 中的元素的顺序是什么?
  3. 能否用不同的 for 循环结构来模拟?
  4. 这个 for 循环可以嵌套一个相似或不同的 for 循环结构吗?它会是什么样子?

也欢迎提供其他信息.

解决方案

最好的信息来源是 关于列表推导式的官方 Python 教程.列表推导式与 for 循环几乎相同(当然,任何列表推导式都可以写成 for 循环),但它们通常比使用 for 循环更快.

从教程中查看这个较长的列表推导式(if 部分过滤推导式,只有通过 if 语句的部分才会被传递到列表推导式的最后部分(这里是 (x,y)):

<预><代码>>>>[(x, y) for x in [1,2,3] for y in [3,1,4] if x != y][(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

它与这个嵌套的 for 循环完全相同(并且,正如教程所说,注意 for 和 if 的顺序是如何相同的).

<预><代码>>>>梳子 = []>>>对于 [1,2,3] 中的 x:... 对于 [3,1,4] 中的 y:...如果 x != y:... combs.append((x, y))...>>>梳子[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

列表推导式和 for 循环之间的主要区别在于 for 循环的最后部分(你做某事的地方)出现在开头而不是结尾.

关于您的问题:

<块引用>

对象必须是什么类型才能使用这个 for 循环结构?

iterable.任何可以生成(有限)元素集的对象.这些包括任何容器、列表、集合、生成器等.

<块引用>

将 i 和 j 分配给 object 中的元素的顺序是什么?

它们的分配顺序与它们从每个列表中生成的顺序完全相同,就好像它们在嵌套的 for 循环中一样(对于您的第一次理解,您将获得 i 的 1 个元素,然后是 j 的每个值,第二个元素到 i,然后是 j 中的每个值,等等)

<块引用>

可以用不同的for循环结构来模拟吗?

是的,上面已经显示了.

<块引用>

这个for循环可以嵌套一个相似或不同的for循环结构吗?它会是什么样子?

当然,但这不是个好主意.例如,这里为您提供了一个字符列表列表:

[[ch for ch in word] for word in ("apple", "banana", "pear", "the", "hello")]

Wrote this function in python that transposes a matrix:

def transpose(m):
    height = len(m)
    width = len(m[0])
    return [ [ m[i][j] for i in range(0, height) ] for j in range(0, width) ]

In the process I realized I don't fully understand how single line nested for loops execute. Please help me understand by answering the following questions:

  1. What is the order in which this for loop executes?
  2. If I had a triple nested for loop, what order would it execute?
  3. What would be equal the equal unnested for loop?

Given,

[ function(i,j) for i,j in object ]

  1. What type must object be in order to use this for loop structure?
  2. What is the order in which i and j are assigned to elements in object?
  3. Can it be simulated by a different for loop structure?
  4. Can this for loop be nested with a similar or different structure for loop? And how would it look?

Additional information is appreciated as well.

解决方案

The best source of information is the official Python tutorial on list comprehensions. List comprehensions are nearly the same as for loops (certainly any list comprehension can be written as a for-loop) but they are often faster than using a for loop.

Look at this longer list comprehension from the tutorial (the if part filters the comprehension, only parts that pass the if statement are passed into the final part of the list comprehension (here (x,y)):

>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

It's exactly the same as this nested for loop (and, as the tutorial says, note how the order of for and if are the same).

>>> combs = []
>>> for x in [1,2,3]:
...     for y in [3,1,4]:
...         if x != y:
...             combs.append((x, y))
...
>>> combs
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

The major difference between a list comprehension and a for loop is that the final part of the for loop (where you do something) comes at the beginning rather than at the end.

On to your questions:

What type must object be in order to use this for loop structure?

An iterable. Any object that can generate a (finite) set of elements. These include any container, lists, sets, generators, etc.

What is the order in which i and j are assigned to elements in object?

They are assigned in exactly the same order as they are generated from each list, as if they were in a nested for loop (for your first comprehension you'd get 1 element for i, then every value from j, 2nd element into i, then every value from j, etc.)

Can it be simulated by a different for loop structure?

Yes, already shown above.

Can this for loop be nested with a similar or different structure for loop? And how would it look?

Sure, but it's not a great idea. Here, for example, gives you a list of lists of characters:

[[ch for ch in word] for word in ("apple", "banana", "pear", "the", "hello")]

这篇关于单行嵌套 For 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆