[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', ...
随机推荐
- HDOJ 2120 Ice_cream's world I
Ice_cream's world I ice_cream's world is a rich country, it has many fertile lands. Today, the queen ...
- 笔记-python-build-in-types
笔记-python-build-in-types 注:文档内容来源为Python 3.6.5 documentation 1. built-in types 1.1. 真值测试 所有对 ...
- Java的多态性Polymorphism
原文地址:http://www.cnblogs.com/jack204/archive/2012/10/29/2745150.html Java中多态性的实现 什么是多态 面向对象的三大特性:封装.继 ...
- 谋哥:App开发者的苦逼不值得怜悯!
[谋哥每天一干货,第四十篇] 为什么取这个标题呢?因为昨天一些本来“支持”谋哥的人看到谋哥搞收费VIP群,觉得谋哥赚苦逼开发者的钱很不道德,且说谋哥我写的东西都不切实际,全部是一些思想性 ...
- N宫格
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...
- 设计模式学习笔记——java中常用的设计模式
单例设计模式(Singleton Pattern) 观察者模式(Observer Pattern) 工厂模式(Factory Pattern) 策略模式(Strategy Pattern) 适配器模式 ...
- 双网卡只有一个能ping通的解决办法
来源:http://blog.csdn.net/centerpoint/article/details/38542719 Linux默认启用了反向路由检查 如果2个网卡在一个Lan里面,那么服务器可能 ...
- [oldboy-django][2深入django]老师管理--查看,添加,编辑
# 添加老师(下拉框多选) 数据库设计: class Teacher(models.Model): name = models.CharField(max_length=64) cls = model ...
- JavaScript: 理解对象
ECMA-262 把对象定义为:“无序属性的集合,其属性可以包含基本值.对象或者函数.” 严格来讲,这就相当于说对象是一组没有特定顺序的值.对象的每个属性或者方法都有一个名字,而每个名字都映射到一个值 ...
- github 下载部分文件夹
1.下载svn: 记得勾上: 2.配置环境变量,将svn的bin目录添加到环境变量 3.svn checkout [链接](你的下载的项目地址) 不过,注意,要改一下哦: 比如我要下载todo项目里的 ...