Toriyung

导航

< 2025年3月 >
23 24 25 26 27 28 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 27 28 29
30 31 1 2 3 4 5

统计

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   Toriyung  阅读(21)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示