首页
随机页面
分类
Python教程
Python HOME]]
Python简介]]
Python入门]]
Python语法]]
Python注释]]
Python变量]]
Python数据类型]]
Python数字]]
Python类型转换]]
Python字符串]]
Python布尔值]]
Python运算符]]
Python列表]]
Python元组]]
Python集合]]
Python字典]]
Python If...Else]]
Python While循环]]
Python For循环]]
Python函数]]
Python Lambda]]
Python数组]]
Python类/对象]]
Python继承]]
Python迭代器]]
Python作用域]]
Python模块]]
Python日期]]
Python JSON]]
Python正则表达式]]
Python PIP包管理]]
Python Try...Except]]
Python输入交互]]
Python字符串格式化]]
文件处理
Python文件处理]]
Python文件读取]]
Python文件读写]]
Python文件删除]]
Python NumPy
NumPy简介]]
NumPy入门]]
NumPy数组创建]]
NumPy数组索引]]
NumPy数组切片]]
NumPy数据类型]]
NumPy数组复制与视图]]
NumPy数组形状]]
NumPy数组重塑]]
NumPy数组迭代]]
NumPy数组连接]]
NumPy数组拆分]]
NumPy数组搜索]]
NumPy数组排序]]
NumPy数组过滤]]
NumPy随机]]
Random简介]]
Data Distribution]]
Random Permutation]]
Seaborn Module]]
Normal Distribution]]
Binomial Distribution]]
Poisson Distribution]]
Uniform Distribution]]
Logistic Distribution]]
Multinomial Distribution]]
Exponential Distribution]]
Chi Square Distribution]]
Rayleigh Distribution]]
Pareto Distribution]]
Zipf Distribution]]
NumPy ufunc]]
ufunc Intro]]
ufunc Create Function]]
ufunc Simple Arithmetic]]
ufunc Rounding Decimals]]
ufunc Logs]]
ufunc Summations]]
ufunc Products]]
ufunc Differences]]
ufunc Finding LCM]]
ufunc Finding GCD]]
ufunc Trigonometric]]
ufunc Hyperbolic]]
ufunc Set Operations]]
机器学习
入门]]
Mean Median Mode]]
Standard Deviation]]
Percentile]]
Data Distribution]]
Normal Data Distribution]]
Scatter Plot]]
Linear Regression]]
Polynomial Regression]]
Multiple Regression]]
Scale]]
Train/Test]]
Decision Tree]]
Python MySQL
MySQL入门]]
MySQL创建数据库]]
MySQL创建表]]
MySQL插入]]
MySQL查询]]
MySQL Where]]
MySQL Order By]]
MySQL Delete]]
MySQL Drop Table]]
MySQL Update]]
MySQL Limit]]
MySQL Join]]
Python MongoDB
MongoDB入门]]
MongoDB创建数据库]]
MongoDB创建集合]]
MongoDB Insert]]
MongoDB Find]]
MongoDB Query]]
MongoDB Sort]]
MongoDB Delete]]
MongoDB Drop Collection]]
MongoDB Update]]
MongoDB Limit]]
Python参考
Python概述]]
Python内建函数]]
Python字符串方法]]
Python列表方法]]
Python字典方法]]
Python元组方法]]
Python集合方法]]
Python文件方法]]
Python关键字]]
Python异常]]
Python词汇表]]
模块参考
随机数模块]]
Requests模块]]
数学模块]]
CMath模块]]
Python How To
删除列表重复项]]
反转字符串]]
数字相加]]
查看“Python/numpy array iterating”的源代码
来自菜鸟教程
←
Python/numpy array iterating
跳转至:
导航
、
搜索
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
<languages /> = NumPy数组迭代 = == 迭代数组 == 迭代意味着一步一步地遍历元素。 当我们在numpy中处理多维数组时,我们可以使用basic <code> for </code> python循环。 如果我们对一维数组进行迭代,它将一一遍历每个元素。 <div> === 例 === 迭代以下一维数组的元素: <pre class="notranslate pythonHigh"> import numpy as np arr = np.array([1, 2, 3]) for x in arr: print(x)</pre> </div> == 迭代二维数组 == 在二维数组中,它将遍历所有行。 <div> === 例 === 迭代以下二维数组的元素: <pre class="notranslate pythonHigh"> import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) for x in arr: print(x)</pre> </div> <div> 如果我们迭代一个 ''n'' -D数组将一一遍历第n-1个维度。 </div> 要返回实际值,标量,我们必须迭代每个维中的数组。 <div> === 例 === 迭代二维数组的每个标量元素: <pre class="notranslate pythonHigh"> import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) for x in arr: for y in x: print(y)</pre> </div> == 迭代3-D阵列 == 在3-D阵列中,它将遍历所有2-D阵列。 <div> === 例 === 迭代以下3-D数组的元素: <pre class="notranslate pythonHigh"> import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) for x in arr: print(x)</pre> </div> 要返回实际值,标量,我们必须迭代每个维中的数组。 <div> === 例 === 迭代到标量: <pre class="notranslate pythonHigh"> import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) for x in arr: for y in x: for z in y: print(z)</pre> </div> == 使用nditer()迭代数组 == 功能 <code> nditer() </code> 是一个帮助功能,可以从非常基本的迭代到非常高级的迭代都可以使用。它解决了我们在迭代中面临的一些基本问题,让我们通过示例进行研究。 === 在每个标量元素上迭代 === 基本 <code> for </code> 循环,遍历我们需要使用的数组的每个标量 ''n'' <code> for </code> 对于具有很高维数的数组可能很难编写循环。 <div> === 例 === 遍历以下3-D数组: <pre class="notranslate pythonHigh"> import numpy as np arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) for x in np.nditer(arr): print(x)</pre> </div> == 具有不同数据类型的迭代数组 == 我们可以用 <code> op_dtypes </code> 参数并将其传递给期望的数据类型,以在迭代时更改元素的数据类型。 NumPy不会就地更改元素的数据类型(该元素位于数组中),因此它需要一些其他空间来执行此操作,该额外空间称为缓冲区,并在其中启用它 <code> nditer() </code> 我们通过 <code> flags=['buffered'] </code> . <div> === 例 === 以字符串形式遍历数组: <pre class="notranslate pythonHigh"> import numpy as np arr = np.array([1, 2, 3]) for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']): print(x)</pre> </div> == 以不同的步长迭代 == 我们可以使用过滤,然后进行迭代。 <div> === 例 === 遍历2D数组的每个标量元素,跳过1个元素: <pre class="notranslate pythonHigh"> import numpy as np arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) for x in np.nditer(arr[:, ::2]): print(x)</pre> </div> == 使用ndenumerate()进行枚举迭代 == 枚举是指一一提及事物的序号。 有时我们在迭代时需要元素的相应索引, <code> ndenumerate() </code> 方法可以用于那些用例。 <div> === 例 === 列举以下一维数组元素: <pre class="notranslate pythonHigh"> import numpy as np arr = np.array([1, 2, 3]) for idx, x in np.ndenumerate(arr): print(idx, x)</pre> </div> <div> === 例 === 枚举以下2D数组的元素: <pre class="notranslate pythonHigh"> import numpy as np arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) for idx, x in np.ndenumerate(arr): print(idx, x)</pre> </div> <br /> [[分类:Python基础教程]]
返回至“
Python/numpy array iterating
”。