pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

“三點"是什么意思?在 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[...] = ... 就地修改了xx = ... 會破壞到 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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 超声波气象站_防爆气象站_空气质量监测站_负氧离子检测仪-风途物联网 | 干粉砂浆设备-干粉砂浆生产线-干混-石膏-保温砂浆设备生产线-腻子粉设备厂家-国恒机械 | 智能楼宇-楼宇自控系统-楼宇智能化-楼宇自动化-三水智能化 | COD分析仪|氨氮分析仪|总磷分析仪|总氮分析仪-圣湖Greatlake | 专业生产动态配料系统_饲料配料系统_化肥配料系统等配料系统-郑州鑫晟重工机械有限公司 | 南京欧陆电气股份有限公司-风力发电机官网| 板框压滤机-隔膜压滤机-厢式压滤机生产厂家-禹州市君工机械设备有限公司 | jrs高清nba(无插件)直播-jrs直播低调看直播-jrs直播nba-jrs直播 上海地磅秤|电子地上衡|防爆地磅_上海地磅秤厂家–越衡称重 | 光泽度计_测量显微镜_苏州压力仪_苏州扭力板手维修-苏州日升精密仪器有限公司 | 门禁卡_智能IC卡_滴胶卡制作_硅胶腕带-卡立方rfid定制厂家 | 都江堰招聘网-都江堰人才网 都江堰人事人才网 都江堰人才招聘网 邢台人才网_邢台招聘网_邢台123招聘【智达人才网】 | 无菌水质袋-NASCO食品无菌袋-Whirl-Pak无菌采样袋-深圳市慧普德贸易有限公司 | 合肥卓创建筑装饰,专业办公室装饰、商业空间装修与设计。 | 新能源汽车电机定转子合装机 - 电机维修设备 - 睿望达 | 悬浮拼装地板_幼儿园_篮球场_悬浮拼接地板-山东悬浮拼装地板厂家 | 定量包装秤,吨袋包装称,伸缩溜管,全自动包装秤,码垛机器人,无锡市邦尧机械工程有限公司 | 骁龙云呼电销防封号系统-axb电销平台-外呼稳定『免费试用』 | 浙江工业冷却塔-菱电冷却塔厂家 - 浙江菱电冷却设备有限公司 | 振动台-振动试验台-振动冲击台-广东剑乔试验设备有限公司 | 门禁卡_智能IC卡_滴胶卡制作_硅胶腕带-卡立方rfid定制厂家 | 甲级防雷检测仪-乙级防雷检测仪厂家-上海胜绪电气有限公司 | 护腰带生产厂家_磁石_医用_热压护腰_登山护膝_背姿矫正带_保健护具_医疗护具-衡水港盛 | 企业彩铃制作_移动、联通、电信集团彩铃上传开通_彩铃定制_商务彩铃管理平台-集团彩铃网 | 电磁流量计_智能防腐防爆管道式计量表-金湖凯铭仪表有限公司 | 罐体电伴热工程-消防管道电伴热带厂家-山东沃安电气 | 山东风淋室_201/304不锈钢风淋室净化设备厂家-盛之源风淋室厂家 翻斗式矿车|固定式矿车|曲轨侧卸式矿车|梭式矿车|矿车配件-山东卓力矿车生产厂家 | 艺术生文化课培训|艺术生文化课辅导冲刺-济南启迪学校 | 座椅式升降机_无障碍升降平台_残疾人升降平台-南京明顺机械设备有限公司 | 单柱拉力机-橡胶冲片机-哑铃裁刀-江都轩宇试验机械厂 | 众品地板网-地板品牌招商_地板装修设计_地板门户的首选网络媒体。 | 二次元影像仪|二次元测量仪|拉力机|全自动影像测量仪厂家_苏州牧象仪器 | 小型UV打印机-UV平板打印机-大型uv打印机-UV打印机源头厂家 |松普集团 | 超细|超微气流粉碎机|气流磨|气流分级机|粉体改性机|磨粉机|粉碎设备-山东埃尔派粉体科技 | 优考试_免费在线考试系统_培训考试系统_题库系统_组卷答题系统_匡优考试 | 冲击式破碎机-冲击式制砂机-移动碎石机厂家_青州市富康机械有限公司 | 新型游乐设备,360大摆锤游乐设备「诚信厂家」-山东方鑫游乐设备 新能源汽车电池软连接,铜铝复合膜柔性连接,电力母排-容发智能科技(无锡)有限公司 | 十二星座查询(性格特点分析、星座运势解读) - 玄米星座网 | 卷筒电缆-拖链电缆-特种柔性扁平电缆定制厂家「上海缆胜」 | 除湿机|工业除湿机|抽湿器|大型地下室车间仓库吊顶防爆除湿机|抽湿烘干房|新风除湿机|调温/降温除湿机|恒温恒湿机|加湿机-杭州川田电器有限公司 | 单螺旋速冻机-双螺旋-流态化-隧道式-食品速冻机厂家-广州冰泉制冷 | 锂电池生产厂家-电动自行车航模无人机锂电池定制-世豹新能源 |