久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

“三點"是什么意思?在 Python 中,當索引什么

What does quot;three dotsquot; in Python mean when indexing what looks like a number?(“三點是什么意思?在 Python 中,當索引什么看起來像一個數字時意味著什么?)
本文介紹了“三點"是什么意思?在 Python 中,當索引什么看起來像一個數字時意味著什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

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

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#modifying-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 的情況下,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 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,)

這會在 a 的行(第一個暗淡)上進行迭代.x 是一維數組,可以使用 x[:]=...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 標志-external-loop" rel="noreferrer">section,x 現在是一維數組,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,)

比較這個簡單的行迭代(在二維數組上):

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 是一維數組,可以使用 x[:] = ...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 是為數不多的非編譯 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 中,當索引什么看起來像一個數字時意味著什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 九九国产在线观看 | 99免费在线观看视频 | 国产高清视频在线观看 | 免费在线看黄视频 | 做a视频在线观看 | 日韩a在线 | 97av视频在线 | 亚洲一区二区三区四区五区午夜 | 久久亚洲国产精品日日av夜夜 | 日韩欧美精品 | 黄色免费av | 国产免费一级一级 | 99久久精品免费看国产四区 | 国产在线精品区 | 丁香婷婷久久久综合精品国产 | 日本欧美在线观看视频 | 天天综合网7799精品 | 波多野结衣一区二区 | 99精品福利视频 | 国产探花 | 国产片侵犯亲女视频播放 | 国产精品乱码一二三区的特点 | 国产精品视频一二三区 | 免费在线一区二区 | 一区二区免费在线视频 | 亚洲精品一区二区三区蜜桃久 | 久久人人爽人人爽人人片av免费 | 91精品国产色综合久久 | 97av视频在线| 91高清在线| 欧美舔穴 | 精品一区电影 | 九九九色| 四虎成人免费电影 | 亚洲成人在线免费 | 国产精品免费大片 | 精品久久久久久久久久久久久 | 欧美一级片在线 | 亚洲精品乱码久久久久久久久 | 亚洲一区二区在线视频 | 精品国产亚洲一区二区三区大结局 |