# NumPy索引 – 索引编制介绍代码示例

2021年3月12日14:16:43 发表评论 328 次浏览

NumPy或Numeric Python是用于在均匀n维数组上进行计算的软件包。在numpy中, 尺寸称为轴。

``````# Python program to demonstrate a need of NumPy

list1 = [1, 2, 3, 4 , 5, 6]
list2 = [10, 9, 8, 7, 6, 5]

# Multiplying both lists directly would give an error.
print(list1*list2)``````

``TypeError: can't multiply sequence by non-int of type 'list'``

``````# Python program to demonstrate the use of NumPy arrays
import numpy as np

list1 = [1, 2, 3, 4, 5, 6]
list2 = [10, 9, 8, 7, 6, 5]

# Convert list1 into a NumPy array
a1 = np.array(list1)

# Convert list2 into a NumPy array
a2 = np.array(list2)

print(a1*a2)``````

``array([10, 18, 24, 28, 30, 30])``

python的Numpy软件包具有以不同方式进行索引的强大功能。

``````# Python program to demonstrate
# the use of index arrays.
import numpy as np

# Create a sequence of integers from
# 10 to 1 with a step of -2
a = np.arange(10, 1, -2)
print("\n A sequential array with a negative step: \n", a)

# Indexes are specified inside the np.array method.
newarr = a[np.array([3, 1, 2 ])]
print("\n Elements at these indices are:\n", newarr)``````

``````A sequential array with a negative step:
[10  8  6  4  2]

Elements at these indices are:
[4 8 6]``````

``````import numpy as np

# NumPy array with elements from 1 to 9
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])

# Index values can be negative.
arr = x[np.array([1, 3, -3])]
print("\n Elements are : \n", arr)``````

``````Elements are:
[2 4 7]``````

#### 索引类型

• 一个切片对象, 其形式为start：stop：step
• 一个整数
• 或切片对象和整数的元组

``````# Python program for basic slicing.
import numpy as np

# Arrange elements from 0 to 19
a = np.arange(20)
print("\n Array is:\n ", a)

# a[start:stop:step]
print("\n a[-8:17:1]  = ", a[-8:17:1])

# The : operator means all elements till the end.
print("\n a[10:]  = ", a[10:])``````

``````Array is:
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]

a[-8:17:1]  =  [12 13 14 15 16]

a[10:] = [10 11 12 13 14 15 16 17 18 19]``````

``````# Python program for basic slicing
# and indexing
import numpy as np

# A 3-Dimensional array
a = np.array([[0, 1, 2, 3, 4, 5]
[6, 7, 8, 9, 10, 11]
[12, 13, 14, 15, 16, 17]
[18, 19, 20, 21, 22, 23]
[24, 25, 26, 27, 28, 29]
[30, 31, 32, 33, 34, 35]]
print("\n Array is:\n ", a)

# slicing and indexing
print("\n a[0, 3:5]  = ", a[0, 3:5])

print("\n a[4:, 4:]  = ", a[4:, 4:])

print("\n a[:, 2]  = ", a[:, 2])

print("\n a[2:;2, ::2]  = ", a[2:;2, ::2])``````

``````Array is:
[[0  1  2  3  4  5]
[6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]
[24 25 26 27 28 29]
[30 31 32 33 34 35]]

a[0, 3:5]  =  [3 4]

a[4:, 4:] = [[28 29], [34 35]]

a[:, 2] =  [2 8 14 20 26 32]

a[2:;2, ::2] = [[12 14 16], [24 26 28]]``````

``````# Python program for indexing using
# basic slicing with ellipsis
import numpy as np

# A 3 dimensional array.
b = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

print(b[..., 1]) #Equivalent to b[: , : , 1 ]``````

``````[[ 2  5]
[ 8 11]]``````

• 整数或布尔类型的ndarray
• 或具有至少一个序列对象的元组
• 是一个非元组序列对象

``````# Python program showing advanced indexing
import numpy as np

a = np.array([[1 , 2 ], [3 , 4 ], [5 , 6 ]])
print(a[[0 , 1 , 2 ], [0 , 0 , 1]])``````

``[1 3 6]``

``````# Python program showing advanced
# and basic indexing
import numpy as np

a = np.array([[0 , 1 , 2], [3 , 4 , 5 ], [6 , 7 , 8], [9 , 10 , 11]])

print(a[1:2 , 1:3 ])
print(a[1:2 , [1, 2]])``````

``````[4, 5]
[4, 5]``````

x [arr1, ：, arr2]

.

x [..., arr1, arr2, ：]

x [arr1, ：, 1]

``````# You may wish to select numbers greater than 50
import numpy as np

a = np.array([10, 40, 80, 50, 100])
print(a[a>50])``````

``[80 100]``

``````# You may wish to square the multiples of 40
import numpy as np

a = np.array([10, 40, 80, 50, 100])
print(a[a%40==0]**2)``````

``[1600 6400])``

``````# You may wish to select those elements whose
# sum of row is a multiple of 10.
import numpy as np

b = np.array([[5, 5], [4, 5], [16, 4]])
sumrow = b.sum(-1)
print(b[sumrow%10==0])``````

``array([[ 5, 5], [16, 4]])``