怪异的python“嵌套”循环

时间:2019-02-08 23:39:04

标签: python loops for-loop

我在另一个人的项目中遇到以下for循环,而且我以前从未见过这样的语法。这有点像嵌套for循环的变异,但不完全是。无论如何,我应该如何解释这一行代码?或者我如何展开这个循环?

for a in [np.transpose(np.array([list(B['v'][x]) + [0,1] for x in (face[0], face[1], face[2])]))  for face in B['shape']]:
    facets.extend([np.do(r) * scale for x in inflate(a)])

1 个答案:

答案 0 :(得分:1)

np.array表达式的内容是:

[list(B['v'][x]) + [0,1] for x in (face[0], face[1], face[2])]

将以上引用为*,正在迭代的外部列表的内容为:

[np.transpose(np.array(*)) for face in B['shape']]

将每个列表理解转换为for循环:

for face in B['shape']:
    y = [] # temporary variable
    for x in (face[0], face[1], face[2]):
        y.append(list(B['v'][x]) + [0, 1])

    # outer loop variable
    a = np.transpose(np.array(y)) 

    z = [] # temporary variable
    for x in inflate(a):
        z.append(np.do(r) * scale)
    facets.extend(z)