简体   繁体   English

为什么 numpy.array 中的元素不替换?

[英]Why element does not replace in numpy.array?

I want to assign a new value to [i + 1][j] of array, but it seems there is a problem.我想为数组的[i + 1][j]分配一个新值,但似乎有问题。

a = np.array(['#', '#', '#', '#', '#', '#','#', ' ', ' ', 'A', ' ', '#',
 '#', ' ', '#', 'P', ' ', '#',
 '#', ' ', ' ', ' ', ' ', '#',
 '#', 'P', ' ', ' ', ' ', '#',
 '#', '2', ' ', ' ', ' ', '#',
 '#', '#', '#', '#', '#', '#'])

print(len(a))
b = np.reshape(a,(7,6))
i = 0
j = 0
print(b[i + 1][j])
b[i + 1][j] = 'AP'
print(b[i + 1][j])

output:输出:

#
A

How can I assign "AP" instead of "#" ?如何分配“AP”而不是“#”?

b.dtype (see data type objects ) is <U1 which is a unicode string of length 1. b.dtype (参见 数据类型对象)是<U1 ,它是一个长度为 1 的 unicode 字符串。

you could fix that with你可以用

a = np.array(['#',..., '#'], dtype='<U2')

which will then accept strings up to length 2.然后将接受长度为 2 的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM