Toriyung

导航

numpy Q&A_1

2022/03/11

 

偶然实践了解到列表和数组的使用上某个区别

 

代码_1

import numpy as np

a = [[1,2,3],[4,5,6]]
b = np.asarray(a)
print('type of b: ' + str(type(b)))
print('type of the element of b: ' + str(type(b[0])))

c = list(b)
print('type of c: ' + str(type(c)))
print('type of the element of c: ' + str(type(c[0])))

 

运行效果_1

 

 

 

代码_2

import numpy as np

a = [1,2,3]
b = np.asarray(a)
print('type of b: ' + str(type(b)))
print('type of the element of b: ' + str(type(b[0])))

c = list(b)
print('type of c: ' + str(type(c)))
print('type of the element of c: ' + str(type(c[0])))

 

运行效果_2

 

 

总结

可以看到,numpy.asarray()进行数组化时将列表整体和列表内的元素一并数组/数组元素化;而list()进行列表化时是对数组整体(或者说外壳)进行列表化,而其内部元素类型仍保持不变(numpy.ndarray/numpy.int32)

posted on 2022-03-11 23:31  Toriyung  阅读(18)  评论(0编辑  收藏  举报