写一个 Python 程序来计算 Numpy 数组的长度。Python numpy 模块有一个返回数组长度的 len 函数。在这个 Python 示例中,我们声明了整数和字符串数组,并使用 len 函数来查找这些长度。
import numpy as np
intarr = np.array([10, 20, 35, 44, 78, 99, 248])
print("Integer Numpy Array Items = ", intarr)
intarrLength = len(intarr)
print("Integer Numpy Array Length = ", intarrLength)
strarr = np.array(['UK', 'India', 'USA', 'Japan'])
print("String Numpy Array Items = ", strarr)
strarrLength = len(strarr)
print("String Numpy Array Length = ", strarrLength)
这个 Python 程序允许输入数组项目并计算 Numpy 数组长度。
import numpy as np
arrList = []
number = int(input("Enter the Total Array Items = "))
for i in range(1, number + 1):
value = int(input("Enter the %d Array value = " %i))
arrList.append(value)
intArr = np.array(arrList)
print("Integer Numpy Array Items = ", intArr)
intArrLength = len(intArr)
print("Integer Numpy Array Length = ", intArrLength)
Python Numpy 数组长度输出
Enter the Total Array Items = 4
Enter the 1 Array value = 20
Enter the 2 Array value = 90
Enter the 3 Array value = 120
Enter the 4 Array value = 50
Integer Numpy Array Items = [ 20 90 120 50]
Integer Numpy Array Length = 4