[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', ...
随机推荐
- 微信SSL证书更换的检查与安装方法
Ubuntu, Debian 查看根证书 确认操作系统上,是否存在以下文件: /etc/ssl/certs/DigiCert_Global_Root_CA.pem /etc/ssl/certs/Bal ...
- Stephen 博客正式开通 【个人公众号:Stephen 】
个人博客开通. 个人公众号:Stephen
- pycharm许可证过期
1.选择enter license 2.选择license server 3.输入http://idea.imsxm.com 4.点击ok 好,解决了
- [python][django学习篇][7]设计博客视图(1)
1上网的流程: 打开浏览器,输入网址(http://zmrenwu.com/) 浏览器根据输入网址,完成以下几件事:1识别服务器地址,2将用户的浏览意图打包成一个http请求,发送给服务器,等待服务器 ...
- hdu6136[模拟+优先队列] 2017多校8
有点麻烦.. /*hdu6136[模拟+优先队列] 2017多校8*/ #include <bits/stdc++.h> using namespace std; typedef long ...
- try 与catch的作用
首先要清楚,如果没有try的话,出现异常会导致程序崩溃.而try则可以保证程序的正常运行下去,比如说: try{ int i = 1/0; }catch(Exception e){ e.printSt ...
- cf 843 A Sorting by Subsequences [建图]
题面: 传送门 思路: 这道题乍一看有点难 但是实际上研究一番以后会发现,对于每一个位置只会有一个数要去那里,还有一个数要离开 那么只要把每个数和他将要去的那个位置连起来,构成了一个每个点只有一个入边 ...
- 二进制包部署Kubernetes集群
今天这篇文章教给大家如何快速部署一套Kubernetes集群.K8S集群部署有几种方式:kubeadm.minikube和二进制包.前两者属于自动部署,简化部署操作,我们这里强烈推荐初学者使用二进制包 ...
- Codeforces Round #361 (Div. 2) B bfs处理
B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- foj 2111 Problem 2111 Min Number
Problem 2111 Min Number Accept: 1025 Submit: 2022Time Limit: 1000 mSec Memory Limit : 32768 KB ...