Python 中的“三个点”在索引一个数字时是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42190783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "three dots" in Python mean when indexing what looks like a number?
提问by Nan Hua
What is the meaning of x[...]
below?
x[...]
下面是什么意思?
a = np.arange(6).reshape(2,3)
for x in np.nditer(a, op_flags=['readwrite']):
x[...] = 2 * x
回答by hpaulj
While the proposed duplicate What does the Python Ellipsis object do?answers the question in a general python
context, its use in an nditer
loop requires, I think, added information.
虽然建议重复Python Ellipsis 对象有什么作用?在一般python
情况下回答这个问题nditer
,我认为它在循环中的使用需要添加信息。
https://docs.scipy.org/doc/numpy/reference/arrays.nditer.html#modifying-array-values
https://docs.scipy.org/doc/numpy/reference/arrays.nditer.html#modifying-array-values
Regular assignment in Python simply changes a reference in the local or global variable dictionary instead of modifying an existing variable in place. This means that simply assigning to x will not place the value into the element of the array, but rather switch x from being an array element reference to being a reference to the value you assigned. To actually modify the element of the array, x should be indexed with the ellipsis.
Python 中的常规赋值只是更改局部或全局变量字典中的引用,而不是修改现有变量。这意味着简单地分配给 x 不会将值放入数组元素中,而是将 x 从数组元素引用切换为对您分配的值的引用。要实际修改数组的元素,应使用省略号对 x 进行索引。
That section includes your code example.
该部分包括您的代码示例。
So in my words, the x[...] = ...
modifies x
in-place; x = ...
would have broken the link to the nditer
variable, and not changed it. It's like x[:] = ...
but works with arrays of any dimension (including 0d). In this context x
isn't just a number, it's an array.
所以用我的话来说,就地x[...] = ...
修改x
; x = ...
会破坏与nditer
变量的链接,而不是更改它。它就像x[:] = ...
但适用于任何维度(包括 0d)的数组。在这种情况下x
,它不仅仅是一个数字,而是一个数组。
Perhaps the closest thing to this nditer
iteration, without nditer
is:
也许与此nditer
迭代最接近的事情nditer
是:
In [667]: for i, x in np.ndenumerate(a):
...: print(i, x)
...: a[i] = 2 * x
...:
(0, 0) 0
(0, 1) 1
...
(1, 2) 5
In [668]: a
Out[668]:
array([[ 0, 2, 4],
[ 6, 8, 10]])
Notice that I had to index and modify a[i]
directly. I could not have used, x = 2*x
. In this iteration x
is a scalar, and thus not mutable
请注意,我必须a[i]
直接索引和修改。我不能使用,x = 2*x
。在这个迭代中x
是一个标量,因此不可变
In [669]: for i,x in np.ndenumerate(a):
...: x[...] = 2 * x
...
TypeError: 'numpy.int32' object does not support item assignment
But in the nditer
case x
is a 0d array, and mutable.
但在这种nditer
情况下x
是一个 0d 数组,并且是可变的。
In [671]: for x in np.nditer(a, op_flags=['readwrite']):
...: print(x, type(x), x.shape)
...: x[...] = 2 * x
...:
0 <class 'numpy.ndarray'> ()
4 <class 'numpy.ndarray'> ()
...
And because it is 0d, x[:]
cannot be used instead of x[...]
并且因为是0d,x[:]
不能代替x[...]
----> 3 x[:] = 2 * x
IndexError: too many indices for array
A simpler array iteration might also give insight:
更简单的数组迭代也可能提供洞察力:
In [675]: for x in a:
...: print(x, x.shape)
...: x[:] = 2 * x
...:
[ 0 8 16] (3,)
[24 32 40] (3,)
this iterates on the rows (1st dim) of a
. x
is then a 1d array, and can be modified with either x[:]=...
or x[...]=...
.
这迭代 . 的行(第一个暗淡)a
。 x
然后是一个一维数组,可以用x[:]=...
或修改x[...]=...
。
And if I add the external_loop
flag from the next section, x
is now a 1d array, and x[:] =
would work. But x[...] =
still works and is more general. x[...]
is used all the other nditer
examples.
如果我添加了external_loop
从下一个标志部分,x
现在是一维数组,并x[:] =
会工作。但x[...] =
仍然有效并且更通用。 x[...]
用于所有其他nditer
示例。
In [677]: for x in np.nditer(a, op_flags=['readwrite'], flags=['external_loop']):
...: print(x, type(x), x.shape)
...: x[...] = 2 * x
[ 0 16 32 48 64 80] <class 'numpy.ndarray'> (6,)
Compare this simple row iteration (on a 2d array):
比较这个简单的行迭代(在二维数组上):
In [675]: for x in a:
...: print(x, x.shape)
...: x[:] = 2 * x
...:
[ 0 8 16] (3,)
[24 32 40] (3,)
this iterates on the rows (1st dim) of a
. x
is then a 1d array, and can be modified with either x[:] = ...
or x[...] = ...
.
这迭代 . 的行(第一个暗淡)a
。 x
然后是一个一维数组,可以用x[:] = ...
或修改x[...] = ...
。
Read and experiment with this nditer
page all the way through to the end. By itself, nditer
is not that useful in python
. It does not speed up iteration - not until you port your code to cython
.np.ndindex
is one of the few non-compiled numpy
functions that uses nditer
.
从头到尾阅读并尝试此nditer
页面。就其本身而言,nditer
在python
. 它不会加速迭代 - 直到您将代码移植到cython
. np.ndindex
是少数numpy
使用nditer
.