什么是“三个点”在Python中意味着索引什么看起来像一个数字? [英] What does "three dots" in Python mean when indexing what looks like a number?

查看:186
本文介绍了什么是“三个点”在Python中意味着索引什么看起来像一个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的x [...]是什么意思?

What is the meaning of x[...] below?

a = np.arange(6).reshape(2,3)
for x in np.nditer(a, op_flags=['readwrite']):
    x[...] = 2 * x


推荐答案

虽然建议重复 Python Ellipsis对象做了什么?在一般 python 上下文中回答了这个问题,我认为,在 nditer 循环中使用需要添加信息。

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.

https://docs.scipy.org/doc/numpy/reference/arrays.nditer。 html #moding-array-values


Python中的常规赋值只是更改本地或全局变量字典中的引用而不是修改现有变量。这意味着简单地分配给x不会将值放入数组的元素中,而是将x作为数组元素引用切换为对指定值的引用。要实际修改数组的元素,x应该用省略号索引。

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.

该部分包含你的代码示例。

That section includes your code example.

所以在我看来, x [...] = ... 修改 x 就地; x = ... 会破坏 nditer 变量的链接,而不会更改它。它类似于 x [:] = ... ,但适用于任何维度的数组(包括0d)。在这种情况下, x 不仅仅是一个数字,而是一个数组。

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.

也许最接近这个 nditer 迭代,没有 nditer 是:

Perhaps the closest thing to this nditer iteration, without nditer is:

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

请注意,我必须直接索引并修改 a [i] 。我无法使用, x = 2 * x 。在此迭代中, x 是标量,因此不可变

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

In [669]: for i,x in np.ndenumerate(a):
     ...:     x[...] = 2 * x
  ...
TypeError: 'numpy.int32' object does not support item assignment

但是在 nditer case x 是一个0d数组,并且是可变的。

But in the nditer case x is a 0d array, and mutable.

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'> ()
...

因为它是0d, x [:] 不能用来代替 x [...]

And because it is 0d, x[:] cannot be used instead of x[...]

----> 3     x[:] = 2 * x
IndexError: too many indices for array

A更简单的数组迭代也可能提供洞察力:

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

这会迭代<$ c的行(第一个暗淡) $ C> A 。 x 是一个1d数组,可以用 x [:] = ... 或<$修改c $ c> x [...] = ... 。

this iterates on the rows (1st dim) of a. x is then a 1d array, and can be modified with either x[:]=... or x[...]=....

如果我添加 external_loop 来自下一个的标志部分 x 现在是1d数组, x [:] = 可以使用。但 x [...] = 仍然有效,而且更为通用。 x [...] 用于所有其他 nditer 示例。

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.

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

比较这个简单的行迭代(在2d数组上):

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

这会迭代 a 的行(第一个暗淡)。 x 是一个1d数组,可以用 x [:] = ... 或<$修改c $ c> x [...] = ... 。

this iterates on the rows (1st dim) of a. x is then a 1d array, and can be modified with either x[:] = ... or x[...] = ....

阅读并试验此 nditer 页面一直到最后。就其本身而言, nditer python 中没有用处。它不会加速迭代 - 直到你将代码移植到 cython np.ndindex 是为数不多的非-compiled numpy 使用 nditer 的函数。

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.

这篇关于什么是“三个点”在Python中意味着索引什么看起来像一个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆