numpy数组元素在为其赋值时不会更改其值

问题描述 投票:0回答:2

我正在拉我的头发。我试图改变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'')]
python arrays numpy
2个回答
1
投票

字符串固定长度为numpy。什么不适合简单地丢弃:

np.array('hello', dtype='S4')
# array(b'hell', dtype='|S4')

dtype('S')似乎相当于dtype('S0')

np.dtype('S').itemsize
# 0

所以分配给你的字符串在0位置被截断。

如果您事先知道预期的最大长度:

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')])

如果您需要灵活的长度,可以使用对象dtype:

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')])

如果你想要足够大的固定长度,手头有所有记录,并且对于你可以为你准备好的确切类型不太挑剔:

lot = [(5,) + tuple('hello world 2 3 4 5 6 7 8 9'.split()), (8,) + tuple('0 1 2 3 short loooooooong 6 7 8 9'.split())]
lot
# [(5, 'hello', 'world', '2', '3', '4', '5', '6', '7', '8', '9'), (8, '0', '1', '2', '3', 'short', 'loooooooong', '6', '7', '8', '9')]
c = np.rec.fromrecords(lot)
c
# rec.array([(5, 'hello', 'world', '2', '3', '4', '5', '6', '7', '8', '9'),
#       (8, '0', '1', '2', '3', 'short', 'loooooooong', '6', '7', '8', '9')], 
#      dtype=[('f0', '<i8'), ('f1', '<U5'), ('f2', '<U5'), ('f3', '<U1'), ('f4', '<U1'), ('f5', '<U5'), ('f6', '<U11'), ('f7', '<U1'), ('f8', '<U1'), ('f9', '<U1'), ('f10', '<U1')])

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)
© www.soinside.com 2019 - 2024. All rights reserved.