我正在尝试理解以下代码:
A = np.arange(3).reshape(3,1)
B = np.arange(3).reshape(1,3)
it = np.nditer([A,B,None])
for x,y,z in it: z[...] = x + y
print(it.operands[2])
我不知道[...]
在做什么。
z
是输出的一个元素。修改它最常见的用例是通过这个语句z[...] = # something
。...
是一个省略号
您的代码基本上是在嵌套的forloop中迭代A
和B
,并将输出写入nditer对象的第三个元素。很像下面的代码。
要阅读有关省略号的更多信息,请参阅https://docs.python.org/dev/library/constants.html#Ellipsis
要阅读有关python-iter输出的更多信息,请参阅https://numpy.org/doc/stable/reference/arrays.nditer.html
特别是Iterator-Allocated Output Arrays
段