编写一个 Python 程序,将 Numpy 数组复制到另一个数组中。在 Python 中,我们可以使用 equals 运算符将完整的 Numpy 数组复制到另一个数组中。
import numpy as np
cparr = np.array([10, 20, 30, 40, 50])
copyarr = cparr
print("***Numpy Arrya Copy Result***")
print("Original Array = ", cparr)
print("Copied Array = ", copyarr)
numpy 数组复制输出
***Numpy Arrya Copy Result***
Original Array = [10 20 30 40 50]
Copied Array = [10 20 30 40 50]
在这个 Python 示例中,我们使用元组切片技术将所需的项复制到另一个 Numpy 数组中。
import numpy as np
cparr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90])
copyarr1 = cparr
copyarr2 = cparr[2:7]
copyarr3 = cparr[3:]
copyarr4 = cparr[::-1]
print("***Numpy Arrya Index Copy Result by Slicing***")
print("Original Array = ", cparr)
print("Copied Array = ", copyarr1)
print("Copy Array from 2 to 7 = ", copyarr2)
print("Copy Array from 3 to End = ", copyarr3)
print("Reverse Array = ", copyarr4)
使用 For 循环范围复制数组的 Python 程序。
import numpy as np
cparr = np.array([12, 22, 35, 55, 47])
copyarr = np.empty(5)
for i in range(len(cparr)):
copyarr[i] = cparr[i]
print("***Numpy Arrya Copy Result***")
print("Original Array = ", cparr)
print("Copied Array = ", copyarr)
使用 进行循环输出
***Numpy Arrya Copy Result***
Original Array = [12 22 35 55 47]
Copied Array = [12\. 22\. 35\. 55\. 47.]