python中 list 与数组的互相转换
(1)list转array: np.array(a)
(2)array 转list a.tolist()
输出结果:
list.append()
list.pop()
list.index()
list.count()
len(list)
list.sort
ndarray.shape 输出一个元组
t2 = np.array([[1, 2, 3], [4, 5, 6]]) # 二维数组
print(t2.shape) #输出元组(2, 3)
print(x.shape[0
])
# 2 只输出行数
print
(x
.shape[
1
])
# 3
print(len(t2)) #2 输出行数
print(len(t2[0])) #3 输出列数
ndarray.size
输出数组元素的总个数,等于shape属性中元组元素的乘积。
ndarray.reshape数组变形
t3 = np.arange(12)
t4 = t3.reshape((3, 4)) # 改变形状,改成3行4列
print(t4)
'''
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
np.where(条件)
返回的是元组,每个元组中的元素是array数组. np.where()并不接受list类型的参数。
—— np.where()[0] 表示行的索引;
—— np.where()[1] 则表示列的索引;
>>> a = np.arange(27).reshape(3,3,3)
>>> a
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
>>> np.where(a > 5)
(array([0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]),
array([2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2]),
array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]))
# 符合条件的元素为
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]]
所以np.where会输出每个元素的对应的坐标,因为原数组有三维,所以tuple中有三个数组。
实例: