# NumPy Python中的基本切片和高级索引

2021年3月30日11:25:25 发表评论 706 次浏览

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 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]``````

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

``````# 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 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]]``````

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

``````# 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]``

``````# 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]])``