0.直接上两段代码:
import numpy as np
a = []
e = 0.3
a.append(['s1', 's2', 's3', float(e)])
a = np.array(a)
print(type(a[0, 3]))
<class 'numpy.str_'>
import numpy as np
a = []
e = 0.3
a.append(['s1', 's2', 's3', float(e)])
a = np.array(a,dtype=object)
print(type(a[0, 3]))
<class 'float'>
1.解释:
数组存储为连续的内存块。它们通常有单一的数据类型(例如整数、浮点数或固定长度的字符串),然后内存中的位被解释为具有该数据类型的值。
创建的数组是不同的。数组占用的内存现在充满了存储在内存其他地方的对象的指针(很像列表实际上只是对象指针的列表,而不是对象本身)。
2.文档原话:
arrays are stored as contiguous blocks of memory. They usually have a single datatype (e.g. integers, floats or fixed-length strings) and then the bits in memory are interpreted as values with that datatype.
Creating an array with is different. The memory taken by the array now is filled with pointers to objects which are being stored elsewhere in memory (much like a list is really just a list of pointers to objects, not the objects themselves).
3.存在的坑点:
如所示,如果多几行后要对矩阵按照第三列排序时使用np.argsort()
函数时会默认按照str
类型的字典序排序,影响最终排序结果!!!!且你如果不懂的话很难发现!!
文章出处登录后可见!
已经登录?立即刷新