python中的列表与数组转换

Python012

python中的列表与数组转换,第1张

列表转换成数组或者数组转换成列表,操作如下(使用函数array 和 tolist):

from numpy import *

listS = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [20, 30, 40, 50, 60, 70, 80, 90, 100]]

print(listS)

temp_array = array(listS, dtype=object)

print(temp_array)

listR = temp_array.tolist()

print(listR)

不是跟2维一样的方法吗,比如

a = [[[1]]]#这是一个现状为(1,1,1)的列表

b = np.array(a)

print(b.shape)#输出(1,1,1)

应该说几维都一样吧,这个