0.直接上两段代码:

Code1:Code1:Code1:

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_'>

Code2:Code2:Code2:

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.解释:

numpynumpynumpy数组存储为连续的内存块。它们通常有单一的数据类型(例如整数、浮点数或固定长度的字符串),然后内存中的位被解释为具有该数据类型的值。
创建dtype=objectdtype=objectdtype=object的数组是不同的。数组占用的内存现在充满了存储在内存其他地方的PythonPythonPython对象的指针(很像PythonPythonPython列表实际上只是对象指针的列表,而不是对象本身)。

2.文档原话:

numpynumpynumpy 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 dtype=objectdtype=objectdtype=object is different. The memory taken by the array now is filled with pointers to PythonPythonPython objects which are being stored elsewhere in memory (much like a PythonPythonPython list is really just a list of pointers to objects, not the objects themselves).

3.存在的坑点:

Code1Code1Code1所示,如果多appendappendappend几行后要对numpynumpynumpy矩阵按照第三列排序时使用np.argsort()函数时pythonpythonpython会默认按照str类型的字典序排序,影响最终排序结果!!!!且你如果不懂的话很难发现!!

22.08.0422.08.0422.08.04
♡Happy Chinese Valentine′s Day chui chui♡\hearts Happy\ Chinese\ Valentine's\ Day_{\ chui\ chui}\heartsHappy Chinese Valentines Day chui chui

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐