0.直接上两段代码:
C o d e 1 : 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_'>
C o d e 2 : 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.解释:
n
u
m
p
y
numpy
numpy数组存储为连续的内存块。它们通常有单一的数据类型(例如整数、浮点数或固定长度的字符串),然后内存中的位被解释为具有该数据类型的值。
创建
d
t
y
p
e
=
o
b
j
e
c
t
dtype=object
dtype=object的数组是不同的。数组占用的内存现在充满了存储在内存其他地方的
P
y
t
h
o
n
Python
Python对象的指针(很像
P
y
t
h
o
n
Python
Python列表实际上只是对象指针的列表,而不是对象本身)。
2.文档原话:
n
u
m
p
y
numpy
numpy 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
d
t
y
p
e
=
o
b
j
e
c
t
dtype=object
dtype=object is different. The memory taken by the array now is filled with pointers to
P
y
t
h
o
n
Python
Python objects which are being stored elsewhere in memory (much like a
P
y
t
h
o
n
Python
Python list is really just a list of pointers to objects, not the objects themselves).
3.存在的坑点:
如
C
o
d
e
1
Code1
Code1所示,如果多
a
p
p
e
n
d
append
append几行后要对
n
u
m
p
y
numpy
numpy矩阵按照第三列排序时使用np.argsort()
函数时
p
y
t
h
o
n
python
python会默认按照str
类型的字典序排序,影响最终排序结果!!!!且你如果不懂的话很难发现!!
22.08.04
22.08.04
22.08.04
♡
H
a
p
p
y
C
h
i
n
e
s
e
V
a
l
e
n
t
i
n
e
′
s
D
a
y
c
h
u
i
c
h
u
i
♡
\hearts Happy\ Chinese\ Valentine's\ Day_{\ chui\ chui}\hearts
♡Happy Chinese Valentine′s Day chui chui♡