标签:个数 array pre 格式 组元 port code div size
Numpy数组创建
import numpy as np ‘‘‘ numpy中的ndarray数组 ‘‘‘ ary = np.array([1, 2, 3, 4, 5]) print(ary) ary = ary * 10 print(ary) ‘‘‘ ndarray对象的创建 ‘‘‘ # 创建二维数组 # np.array([[],[],...]) a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) print(a) # np.arange(起始值, 结束值, 步长(默认1)) b = np.arange(1, 10, 1) print(b) # np.zeros(数组元素个数, dtype=‘数组元素类型‘) c = np.zeros(10) print(c, ‘; c.dtype:‘, c.dtype) # np.ones(数组元素个数, dtype=‘数组元素类型‘) d = np.ones(10, dtype=‘int64‘) print(d, ‘; d.dtype:‘, d.dtype)
Numpy的ndarray对象属性:
数组的维度:array.shape
元素的类型:array.dtype
数组元素的个数:array.size
数组的索引(下标):array[0]
‘‘‘ 数组的基本属性 ‘‘‘ a = np.array([[1, 2, 3], [4, 5, 6]]) print(a)
# 测试数组的基本属性 print(‘a.shape:‘, a.shape) # a.shape = (6, ) # 此格式可将原数组结构变成1行6列的数据结构 # print(a, ‘a.shape:‘, a.shape) print(‘a.size:‘, a.size) print(‘len(a):‘, len(a)) # 数组元素的索引 ary = np.arange(1, 28) ary.shape = (3, 3, 3) # 创建三维数组 print(ary, ‘; ary.shape:‘, ary.shape) print(‘ary[0]:‘, ary[0]) print(‘ary[0][0]:‘, ary[0][0]) print(‘ary[0][0][0]:‘, ary[0][0][0]) print(‘ary[0,0,0]:‘, ary[0, 0, 0]) # 遍历三维数组 for i in range(ary.shape[0]): for j in range(ary.shape[1]): for k in range(ary.shape[2]): print(ary[i, j, k], end=‘ ‘)
标签:个数 array pre 格式 组元 port code div size
原文地址:https://www.cnblogs.com/wodexk/p/10308090.html