如何在某些索引位置获取 NumPy 数组的值?
有时我们需要从源 Numpy 数组中删除值并将它们添加到目标数组中的特定索引处。在 NumPy 中,我们有这种灵活性,我们可以从一个数组中删除值并将它们添加到另一个数组中。我们可以使用numpy.put()函数执行此操作,它可以应用于所有形式的数组,如一维、二维等。
示例 1:
Python3
# Importing Numpy module
import numpy as np
# Creating 1-D Numpy array
a1 = np.array([11, 10, 22, 30, 33])
print("Array 1 :")
print(a1)
a2 = np.array([1, 15, 60])
print("Array 2 :")
print(a2)
print("\nTake 1 and 15 from Array 2 and put them in\
1st and 5th position of Array 1")
a1.put([0, 4], a2)
print("Resultant Array :")
print(a1)
Python3
# Importing Numpy module
import numpy as np
# Creating 2-D Numpy array
a1 = np.array([[11, 10, 22, 30],
[14, 58, 88, 100]])
print("Array 1 :")
print(a1)
a2 = np.array([1, 15, 6, 40])
print("Array 2 :")
print(a2)
print("\nTake 1, 15 and 6 from Array 2 and put them in 1st,\
4th and 7th positions of Array 1")
a1.put([0, 3, 6], a2)
print("Resultant Array :")
print(a1)
Python3
# Importing Numpy module
import numpy as np
# Creating 3-D Numpy array
a1 = np.array([[[11, 25, 7], [30, 45, 55], [20, 45, 7]],
[[50, 65, 8], [70, 85, 10], [11, 22, 33]],
[[19, 69, 36], [1, 5, 24], [4, 20, 9]]])
print("Array 1 :")
print(a1)
# Creating 2-D array
a2 = np.array([[1, 15, 10],
[6, 40, 50],
[11, 5, 10]])
print("\nArray 2 :")
print(a2)
print("\nTake 1, 15, 10, 6, 40 and 50 from Array 2 and put\
them in 1st, 3rd, 5th, 9th, 11th and 15th positions of Array 1")
a1.put([0, 2, 4, 8, 10, 14], a2)
print("Resultant Array :")
print(a1)
Python3
# Importing Numpy module
import numpy as np
# Creating 2-D Numpy array
a1 = np.array([[11, 10, 22],
[14, 58, 88]])
print("Array 1 :")
print(a1)
a2 = np.array([[1, 15, 6],
[40, 50, 70]])
print("Array 2 :")
print(a2)
print("\nTake 1 and 15 from Array 2 and put them in 1st \
and 5th positions of Array 1")
print("by mistake we write the index which is out of bound,\
now mode will play its role")
a1.put([0, 15], a2, mode='raise')
print("\nResultant Array :")
print(a1)
Python3
# Importing Numpy module
import numpy as np
# Creating 2-D Numpy array
a1 = np.array([[11, 10, 22],
[14, 58, 88]])
print("Array 1 :")
print(a1)
a2 = np.array([[1, 15, 6],
[40, 50, 70]])
print("Array 2 :")
print(a2)
print("\nTake 1 and 15 from Array 2 and put them in 1st and\
5th positions of Array 1")
print("by mistake we write the index which is out of bound,\
now mode will play its role")
a1.put([0, 15], a2, mode = 'clip')
print("\nResultant Array :")
print(a1)
输出:
在上面的例子中,我们采用两个一维数组,并在特定位置将值从一个数组传输到另一个数组。
示例 2:
蟒蛇3
# Importing Numpy module
import numpy as np
# Creating 2-D Numpy array
a1 = np.array([[11, 10, 22, 30],
[14, 58, 88, 100]])
print("Array 1 :")
print(a1)
a2 = np.array([1, 15, 6, 40])
print("Array 2 :")
print(a2)
print("\nTake 1, 15 and 6 from Array 2 and put them in 1st,\
4th and 7th positions of Array 1")
a1.put([0, 3, 6], a2)
print("Resultant Array :")
print(a1)
输出:
在上面的示例中,我们采用两个不同的数组,并在特定位置将值从一维数组传输到二维数组。
示例 3:
蟒蛇3
# Importing Numpy module
import numpy as np
# Creating 3-D Numpy array
a1 = np.array([[[11, 25, 7], [30, 45, 55], [20, 45, 7]],
[[50, 65, 8], [70, 85, 10], [11, 22, 33]],
[[19, 69, 36], [1, 5, 24], [4, 20, 9]]])
print("Array 1 :")
print(a1)
# Creating 2-D array
a2 = np.array([[1, 15, 10],
[6, 40, 50],
[11, 5, 10]])
print("\nArray 2 :")
print(a2)
print("\nTake 1, 15, 10, 6, 40 and 50 from Array 2 and put\
them in 1st, 3rd, 5th, 9th, 11th and 15th positions of Array 1")
a1.put([0, 2, 4, 8, 10, 14], a2)
print("Resultant Array :")
print(a1)
输出:
在上面的示例中,我们采用两个不同的数组并将值从 2-D 数组传输到特定位置的 3-D 数组。出现一个问题,如果出现越界索引会发生什么。为此,我们在 numpy.put()函数中有 3 种模式
mode = {‘raise’, ‘wrap’, ‘clip’}
- ‘raise’ – raise an error (default)
- ‘wrap’ – wrap around
- ‘clip’ – clip to the range
示例 4:采取模式 = 'raise'
蟒蛇3
# Importing Numpy module
import numpy as np
# Creating 2-D Numpy array
a1 = np.array([[11, 10, 22],
[14, 58, 88]])
print("Array 1 :")
print(a1)
a2 = np.array([[1, 15, 6],
[40, 50, 70]])
print("Array 2 :")
print(a2)
print("\nTake 1 and 15 from Array 2 and put them in 1st \
and 5th positions of Array 1")
print("by mistake we write the index which is out of bound,\
now mode will play its role")
a1.put([0, 15], a2, mode='raise')
print("\nResultant Array :")
print(a1)
输出:
在上面的示例中,当索引超出范围时,mode='raise' 会生成错误。
示例 5:采用模式 = 'clip'
蟒蛇3
# Importing Numpy module
import numpy as np
# Creating 2-D Numpy array
a1 = np.array([[11, 10, 22],
[14, 58, 88]])
print("Array 1 :")
print(a1)
a2 = np.array([[1, 15, 6],
[40, 50, 70]])
print("Array 2 :")
print(a2)
print("\nTake 1 and 15 from Array 2 and put them in 1st and\
5th positions of Array 1")
print("by mistake we write the index which is out of bound,\
now mode will play its role")
a1.put([0, 15], a2, mode = 'clip')
print("\nResultant Array :")
print(a1)
输出:
在上面的示例中, mode='clip' 替换沿轴的最后一个元素,而不是引发任何错误。