[Python Cookbook] Numpy: Iterating Over Arrays
1. Using for-loop
Iterate along row axis:
import numpy as np
x=np.array([[1,2,3],[4,5,6]])
for i in x:
print(x)
Output:
[1 2 3]
[4 5 6]
Iterate by index:
for i in range(len(x)):
print(x[i])
Output:
[1 2 3]
[4 5 6]
Iterate by row and index:
for i, row in enumerate(x):
print('row', i, 'is', row)
Output:
row 0 is [1 2 3]
row 1 is [4 5 6]
2. Using ndenumerate object
for index, i in np.ndenumerate(x):
print(index,i)
Output:
(0, 0) 1
(0, 1) 2
(0, 2) 3
(1, 0) 4
(1, 1) 5
(1, 2) 6
3. Using nditer object
See: https://docs.scipy.org/doc/numpy-1.15.0/reference/arrays.nditer.html
4. Use zip
to iterate over multiple iterables
x2 = x**2
print(x2,'\n')
for i, j in zip(x, x2):
print(i,'+',j,'=',i+j)
Output:
[[ 1 4 9]
[16 25 36]]
[1 2 3] + [1 4 9] = [ 2 6 12]
[4 5 6] + [16 25 36] = [20 30 42]
[Python Cookbook] Numpy: Iterating Over Arrays的更多相关文章
- [Python Cookbook] Numpy Array Joint Methods: Append, Extend & Concatenate
数组拼接方法一 思路:首先将数组转成列表,然后利用列表的拼接函数append().extend()等进行拼接处理,最后将列表转成数组. 示例1: import numpy as np a=np.arr ...
- [Python Cookbook] Numpy: How to Apply a Function to 1D Slices along the Given Axis
Here is a function in Numpy module which could apply a function to 1D slices along the Given Axis. I ...
- [Python Cookbook] Numpy: Multiple Ways to Create an Array
Convert from list Apply np.array() method to convert a list to a numpy array: import numpy as np myl ...
- [Python Cookbook] Numpy Array Slicing and Indexing
1-D Array Indexing Use bracket notation [ ] to get the value at a specific index. Remember that inde ...
- [Python Cookbook] Numpy Array Manipulation
1. Reshape: The np.reshape() method will give a new shape to an array without changing its data. Not ...
- numpy 数组迭代Iterating over arrays
在numpy 1.6中引入的迭代器对象nditer提供了许多灵活的方式来以系统的方式访问一个或多个数组的所有元素. 1 单数组迭代 该部分位于numpy-ref-1.14.5第1.15 部分Singl ...
- [转]python与numpy基础
来源于:https://github.com/HanXiaoyang/python-and-numpy-tutorial/blob/master/python-numpy-tutorial.ipynb ...
- Python之Numpy详细教程
NumPy - 简介 NumPy 是一个 Python 包. 它代表 “Numeric Python”. 它是一个由多维数组对象和用于处理数组的例程集合组成的库. Numeric,即 NumPy 的前 ...
- 【python cookbook】找出序列中出现次数最多的元素
问题 <Python Cookbook>中有这么一个问题,给定一个序列,找出该序列出现次数最多的元素.例如: words = [ 'look', 'into', 'my', 'eyes', ...
随机推荐
- python项目中输出指定颜色的日志
起因 在开发项目过程中,为了方便调试代码,经常会向stdout中输出一些日志,默认的这些日志就直接显示在了终端中.而一般的应用服务器,第三方库,甚至服务器的一些通告也会在终端中显示,这样就搅乱了我们想 ...
- 求 1 到 n 的所有数的约数和
求 1 到 n 的所有数的约数和 暴力方法就是枚举每个数,算出他的约数和即可,这样有点慢. 另一种思路,枚举约数,判断他是谁的约数,并记录(即他的倍数有多少个),在乘以他自己. n/i求的是n以内,i ...
- 转投emacs
(global-set-key [f9] 'compile-file) (global-set-key [f10] 'gud-gdb) (global-set-key (kbd "C-z&q ...
- RemoteFX
RemoteFX 编辑 RemoteFX是微软在Windows 7/2008 R2 SP1中增加的一项桌面虚拟化技术,使得用户在使用远程桌面或虚拟桌面进行游戏应用时,可以获得和本地桌面一致的效果. 外 ...
- hnust 心电图
问题 A: 心电图 时间限制: 1 Sec 内存限制: 128 MB提交: 621 解决: 250[提交][状态][讨论版] 题目描述 众所周知,ACM/ICPC实验室聚集了一堆学霸Orz 有学霸 ...
- SQL Server 2014存储过程的备份和还原
Sql Server 2014存储过程备份和恢复... 1 1. 备份存储过程:... 1 2. 还原... 8 Sql Server 2014存储过程备份和恢复 1. 备份存储过 ...
- zookeeper 下载安装
下载:wget https://www-us.apache.org/dist/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz 解压:tar -zx ...
- ABC128F Frog Jump
题目链接 题目大意 给定一个长为 $n$ 的数组 $s$,下标从 $0$ 开始.$ 3 \le n \le 10^5$,$-10^9 \le s_i \le 10^9$,$s_0 = s_{n - 1 ...
- BZOJ2916 [Poi1997]Monochromatic Triangles 数论
答案等于总三角形数-不合法数 一个不合法三角形一定存在两个顶点,在这个三角形中这个顶点的角的两边不同色 #include<cstring> #include<cmath> #i ...
- web储存用户信息
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...