我拉我的头发。我试图改变numpy数组的元素无济于事:
import numpy as np
c = np.empty((1), dtype='i4, S, S, S, S, S, S, S, S, S')
print(c)
c[0][1]="hello"
c[0][2]='hello'
c[0][3]=b'hello'
print(c)
输出:
[(0, b'', b'', b'', b'', b'', b'', b'', b'', b'')]
[(0, b'', b'', b'', b'', b'', b'', b'', b'', b'')]
答案 0 :(得分:1)
字符串是固定长度的numpy。什么不适合被简单地丢弃:
class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL)
np.array('hello', dtype='S4')
# array(b'hell', dtype='|S4')
似乎等同于dtype('S')
:
dtype('S0')
因此,分配给你的字符串会被截断到位置np.dtype('S').itemsize
# 0
。
如果您事先知道预期的最大长度:
0
如果您需要灵活的长度,可以使用对象dtype:
c = np.empty((1,), dtype=', '.join(['i4'] + 9*['S5']))
for i in range(1, 10):
c[0][i] = 'hello'
c
# array([ (-1710610776, b'hello', b'hello', b'hello', b'hello', b'hello', b'hello', b'hello', b'hello', b'hello')],
# dtype=[('f0', '<i4'), ('f1', 'S5'), ('f2', 'S5'), ('f3', 'S5'), ('f4', 'S5'), ('f5', 'S5'), ('f6', 'S5'), ('f7', 'S5'), ('f8', 'S5'), ('f9', 'S5')])
如果你想要足够大的固定长度,手头有所有记录,并且对于你可以为你准备的确切类型不太挑剔:
c = np.empty((1,), dtype=', '.join(['i4'] + 9*['O']))
for i in range(1, 10):
c[0][i] = 'hello world'[:i]
c
# array([ (0, 'h', 'he', 'hel', 'hell', 'hello', 'hello ', 'hello w', 'hello wo', 'hello wor')],
# dtype=[('f0', '<i4'), ('f1', 'O'), ('f2', 'O'), ('f3', 'O'), ('f4', 'O'), ('f5', 'O'), ('f6', 'O'), ('f7', 'O'), ('f8', 'O'), ('f9', 'O')])
答案 1 :(得分:0)
您正在使用长度为0的字符串。您必须使字段足够大,以便显示文本:
import numpy as np
c = np.empty((1), dtype='i4, S5, S5, S5, S5, S5, S5, S5, S5, S5')
print(c)
c[0][1]="hello"
c[0][2]='hello'
c[0][3]=b'hello'
print(c)