2

[python list和array 的区别]

 3 years ago
source link: https://segmentfault.com/a/1190000040609938
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

[python list和array 的区别]

发布于 43 分钟前

本质上是在探讨两种数据类型的不同

  1. 在计算机里数据结构是怎么存储的,譬如在C++中int x=3,和vector<int> x
    两者之间计算机进行了什么处理?
  2. Python 中数据存储的过程和c++中的一样吗?python 中x=np.array([2,1,2])中和c++中声明的一样吗?
  3. 在学习两种类型转换的方法时,应该多考虑在什么时候和背景下用到转换?
  • list:中保存的是数据的存放的地址,即指针,并非数据。
    array() 是 numpy 包中的一个函数,所以才会有np.array()这种写法,array 里的元素都是同一类型。
x=np.array([[0,1,2,3],'abcd'])
print(type(x))

'''
输出
'''
[list([0, 1, 2, 3]) 'abcd'] 
<class 'numpy.ndarray'>

为什么还是可以执行成功,并且type(x)为numpy.ndarray,type(x[0])<class 'list'>,type(x[1])为<class 'str'>,说法存在问题。

  • 转换
    ` a = np.array([2,3])
    b = a.tolist()`
    图片.png

    ` a = [2, 3]
    b = np.array(a)`
    图片.png


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK