我在别人的项目中遇到过以下循环,我以前从未见过这样的语法。这有点像嵌套 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)