简体   繁体   中英

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.

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 "#" ?

b.dtype (see data type objects ) is <U1 which is a unicode string of length 1.

you could fix that with

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

which will then accept strings up to length 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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