当前位置:网站首页>Numpy array index slice
Numpy array index slice
2022-07-06 01:38:00 【Lao Xiao of Buddhism】
List of articles
NumPy Array index
Accessing array elements
Array index is the same as accessing array elements .
You can access an array element by referencing its index number .
NumPy The index in the array is in 0 start , This means that the index of the first element is 0, The index of the second element is 1, And so on .
example 1 Get the first element from the following array :
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[0])
Running results :
1
example 2 Get the second element from the following array .
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[1])
Running results :
2
example 3 Get the third and fourth elements from the following array and add them .
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[2] + arr[3])
Running results :
7
visit 2-D Array
To access elements from a two-dimensional array , We can use comma separated integers to represent the dimension and index of elements .
Think of a two-dimensional array as a table containing rows and columns , Where rows represent dimensions , The index represents the column .
example : Access the elements on the first row and the second column :
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print(' The elements on the first row and the second column are : ', arr[0, 1])
Running results :
The elements on the first row and the second column are : 2
example 2: Access No. 2 Xing di 5 Elements in columns :
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print(' The first 2 Xing di 5 The elements in the column are : ', arr[1, 4])
Running results :
The first 2 Xing di 5 The elements in the column are : 10
visit 3-D Array
To access elements from a 3D array , We can use comma separated integers to represent the dimension and index of elements .
example 1 Access the third element of the second array of the first array :
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2])
Running results :
6
example
The first number represents the first dimension , It contains two arrays :
[[1, 2, 3], [4, 5, 6]]
and :
[[7, 8, 9], [10, 11, 12]]
Because we chose 0
, Represents that we have selected the array :
[[1, 2, 3], [4, 5, 6]]
The second number represents the second dimension , It also contains two arrays :
[1,2,3]
and :
[4,5,6]
Because we chose 1
, Represents that we selected the second array :
[4,5,6]
The third number represents the third dimension , It contains three values :
4
5
6
Because we chose 2
, We finally get the third value :
6
Negative index
Use a negative index to access the array from the end .
example Print the second dim Last element of :
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print(' the second dim Last element of : ', arr[1, -1])
Running results :
the second dim Last element of : 10
NumPy Array slice
Slice array
Python Slicing in means bringing elements from one given index to another .
We pass slices instead of indexes , As shown below :.[start:end]
We can also define steps , As shown below :.[start:end:step]
If we don't set start, Says from the 0 Start
If we don't set end, Indicates the length of the array
If we don't set step, The default is 1
example : Index the elements from the following array 1 Slice to index 5:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5])
Running results :
[2 3 4 5]
Be careful : result Include
Starting index , but barring
End index .
example : Remove elements from the index 4 Slice to the end of the array :
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[4:])
Running results :
[5 6 7]
Example : From start to index 4( barring ) Slice elements :
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[:4])
Running results :
[1 2 3 4]
Negative slice
Use the minus operator to reference the index from the end :
example : Index from the end 3 Cut to the index from the end 1:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[-3:-1])
Running results :
[5 6]
step step
Use step
Determine the slice length :
example : Return from index 1 To the index 5 All other elements of :
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5:2])
Running results :
[2 4]
Example Returns all odd elements from the entire array :
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[::2])
Running results :
[1 3 5 7]
2-D Array section
example Start with the second element , Remove elements from the index 1 Slice to index 4( Not included ):
import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[1, 1:4])
Running results :
[7 8 9]
example Return the index from these two elements 2:
import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, 2])
Running results :
[3 8]
Example : From two elements , Slice indices 1 To the index 4( Not included ), This will return to a 2-D Array :
import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, 1:4])
Running results :
[[2 3 4]
[7 8 9]]
边栏推荐
- Cadre du Paddle: aperçu du paddlelnp [bibliothèque de développement pour le traitement du langage naturel des rames volantes]
- Nmap: network detection tool and security / port scanner
- Kotlin basics 1
- 什么是弱引用?es6中有哪些弱引用数据类型?js中的弱引用是什么?
- Huawei converged VLAN principle and configuration
- Reasonable and sensible
- PHP error what is an error?
- Basic operations of databases and tables ----- unique constraints
- [solved] how to generate a beautiful static document description page
- Leetcode skimming questions_ Invert vowels in a string
猜你喜欢
3D视觉——4.手势识别(Gesture Recognition)入门——使用MediaPipe含单帧(Singel Frame)和实时视频(Real-Time Video)
【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
A picture to understand! Why did the school teach you coding but still not
leetcode刷题_反转字符串中的元音字母
A Cooperative Approach to Particle Swarm Optimization
Open source | Ctrip ticket BDD UI testing framework flybirds
Force buckle 9 palindromes
Alibaba canal usage details (pit draining version)_ MySQL and ES data synchronization
Mathematical modeling learning from scratch (2): Tools
NiO related knowledge (II)
随机推荐
Dynamics 365 开发协作最佳实践思考
ClickOnce does not support request execution level 'requireAdministrator'
[flask] official tutorial -part1: project layout, application settings, definition and database access
[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.
Basic operations of databases and tables ----- unique constraints
Leetcode skimming questions_ Sum of squares
Unreal browser plug-in
Mongodb problem set
Nmap: network detection tool and security / port scanner
How to get the PHP version- How to get the PHP Version?
[机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。
插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
【网络攻防实训习题】
MUX VLAN configuration
现货白银的一般操作方法
MATLB|实时机会约束决策及其在电力系统中的应用
【已解决】如何生成漂亮的静态文档说明页
NLP fourth paradigm: overview of prompt [pre train, prompt, predict] [Liu Pengfei]
500 lines of code to understand the principle of mecached cache client driver
Leetcode 208. 实现 Trie (前缀树)