【问题标题】:what does Z[...] mean in python? (Z is an array) [duplicate]Z[...] 在 python 中是什么意思? (Z是一个数组)[重复]
【发布时间】:2021-07-31 02:40:18
【问题描述】:

我试图理解以下代码:

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])

我不知道[...] 在做什么。

【问题讨论】:

标签: python numpy


【解决方案1】:

z 是您输出的一个元素。修改它的最常见用例是通过此语句z[...] = # something... 是省略号。
您的代码所做的基本上是在嵌套的 forloop 中迭代 AB,并将输出写入 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部分

【讨论】:

  • nditer docs 有两个主要问题。它声称是“高效的”,但它忽略了关键细节,例如迭代变量的性质。太多的初学者被误导了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
  • 2014-10-01
  • 1970-01-01
  • 2012-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多