奇怪的蟒蛇“嵌套” for循环

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了奇怪的蟒蛇“嵌套” 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)])
答案

所述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)

以上是关于奇怪的蟒蛇“嵌套” for循环的主要内容,如果未能解决你的问题,请参考以下文章