I have a structured NumPy array:
我有一个结构化的NumPy数组:
a = numpy.zeros((10, 10), dtype=[
("x", int),
("y", str)])
I want to set values in a["y"]
to either "hello"
if the corresponding value in a["x"]
is negative. As far as I can tell, I should be doing that like this:
如果[x]中的对应值为负值,我想将[y]中的值设置为"hello"。就我所知,我应该这样做:
a["y"][a["x"] < 0] = "hello"
But this seems to change the values in a["x"]
! What is the problem with what I'm doing, and how else should I do this?
但这似乎改变了[x]的值!我正在做的事情有什么问题,我该怎么做呢?
1 个解决方案
#1
5
First of all, in numpy structured arrays, when you specify datatype as str
numpy
assumes it to be a 1 character string.
首先,在numpy结构数组中,当您指定数据类型为str numpy时,假设它是一个1字符字符串。
>>> a = numpy.zeros((10, 10), dtype=[
("x", int),
("y", str)])
>>> print a.dtype
dtype([('x', '<i8'), ('y', 'S')])
As a result the values you enter get truncated to 1 character.
因此,您输入的值将被截断为1个字符。
>>> a["y"][0][0] = "hello"
>>> print a["y"][0][0]
h
Hence use data type as a10
, Where 10 being the max length of your string.
因此使用数据类型作为a10,其中10是字符串的最大长度。
Refer this link, which specifies more definitions for other data structures.
请参考此链接,它为其他数据结构指定了更多的定义。
Secondly your approach seems correct to me.
第二,你的方法在我看来是正确的。
Inititating a structured numpy array with datatype int and a10
使用datatype int和a10初始化一个结构化的numpy数组。
>>> a = numpy.zeros((10, 10), dtype=[("x", int), ("y", 'a10')])
Filling it with random numbers
用随机数填充它
>>> a["x"][:] = numpy.random.randint(-10, 10, (10,10))
>>> print a["x"]
[[ 2 -4 -10 -3 -4 4 3 -8 -10 2]
[ 5 -9 -4 -1 9 -10 3 0 -8 2]
[ 5 -4 -10 -10 -1 -8 -1 0 8 -4]
[ -7 -3 -2 4 6 6 -8 3 -8 8]
[ 1 2 2 -6 2 -9 3 6 6 -6]
[ -6 2 -8 -8 4 5 8 7 -5 -3]
[ -5 -1 -1 9 5 -7 2 -2 -9 3]
[ 3 -10 7 -8 -4 -2 -4 8 5 0]
[ 5 6 5 8 -8 5 -10 -6 -2 1]
[ 9 4 -8 6 2 4 -10 -1 9 -6]]
Applying your filtering
运用你的过滤
>>> a["y"][a["x"]<0] = "hello"
>>> print a["y"]
[['' 'hello' 'hello' 'hello' 'hello' '' '' 'hello' 'hello' '']
['' 'hello' 'hello' 'hello' '' 'hello' '' '' 'hello' '']
['' 'hello' 'hello' 'hello' 'hello' 'hello' 'hello' '' '' 'hello']
['hello' 'hello' 'hello' '' '' '' 'hello' '' 'hello' '']
['' '' '' 'hello' '' 'hello' '' '' '' 'hello']
['hello' '' 'hello' 'hello' '' '' '' '' 'hello' 'hello']
['hello' 'hello' 'hello' '' '' 'hello' '' 'hello' 'hello' '']
['' 'hello' '' 'hello' 'hello' 'hello' 'hello' '' '' '']
['' '' '' '' 'hello' '' 'hello' 'hello' 'hello' '']
['' '' 'hello' '' '' '' 'hello' 'hello' '' 'hello']]
Verifying a["x"]
验证一个[x]
>>> print a["x"]
[[ 2 -4 -10 -3 -4 4 3 -8 -10 2]
[ 5 -9 -4 -1 9 -10 3 0 -8 2]
[ 5 -4 -10 -10 -1 -8 -1 0 8 -4]
[ -7 -3 -2 4 6 6 -8 3 -8 8]
[ 1 2 2 -6 2 -9 3 6 6 -6]
[ -6 2 -8 -8 4 5 8 7 -5 -3]
[ -5 -1 -1 9 5 -7 2 -2 -9 3]
[ 3 -10 7 -8 -4 -2 -4 8 5 0]
[ 5 6 5 8 -8 5 -10 -6 -2 1]
[ 9 4 -8 6 2 4 -10 -1 9 -6]]
#1
5
First of all, in numpy structured arrays, when you specify datatype as str
numpy
assumes it to be a 1 character string.
首先,在numpy结构数组中,当您指定数据类型为str numpy时,假设它是一个1字符字符串。
>>> a = numpy.zeros((10, 10), dtype=[
("x", int),
("y", str)])
>>> print a.dtype
dtype([('x', '<i8'), ('y', 'S')])
As a result the values you enter get truncated to 1 character.
因此,您输入的值将被截断为1个字符。
>>> a["y"][0][0] = "hello"
>>> print a["y"][0][0]
h
Hence use data type as a10
, Where 10 being the max length of your string.
因此使用数据类型作为a10,其中10是字符串的最大长度。
Refer this link, which specifies more definitions for other data structures.
请参考此链接,它为其他数据结构指定了更多的定义。
Secondly your approach seems correct to me.
第二,你的方法在我看来是正确的。
Inititating a structured numpy array with datatype int and a10
使用datatype int和a10初始化一个结构化的numpy数组。
>>> a = numpy.zeros((10, 10), dtype=[("x", int), ("y", 'a10')])
Filling it with random numbers
用随机数填充它
>>> a["x"][:] = numpy.random.randint(-10, 10, (10,10))
>>> print a["x"]
[[ 2 -4 -10 -3 -4 4 3 -8 -10 2]
[ 5 -9 -4 -1 9 -10 3 0 -8 2]
[ 5 -4 -10 -10 -1 -8 -1 0 8 -4]
[ -7 -3 -2 4 6 6 -8 3 -8 8]
[ 1 2 2 -6 2 -9 3 6 6 -6]
[ -6 2 -8 -8 4 5 8 7 -5 -3]
[ -5 -1 -1 9 5 -7 2 -2 -9 3]
[ 3 -10 7 -8 -4 -2 -4 8 5 0]
[ 5 6 5 8 -8 5 -10 -6 -2 1]
[ 9 4 -8 6 2 4 -10 -1 9 -6]]
Applying your filtering
运用你的过滤
>>> a["y"][a["x"]<0] = "hello"
>>> print a["y"]
[['' 'hello' 'hello' 'hello' 'hello' '' '' 'hello' 'hello' '']
['' 'hello' 'hello' 'hello' '' 'hello' '' '' 'hello' '']
['' 'hello' 'hello' 'hello' 'hello' 'hello' 'hello' '' '' 'hello']
['hello' 'hello' 'hello' '' '' '' 'hello' '' 'hello' '']
['' '' '' 'hello' '' 'hello' '' '' '' 'hello']
['hello' '' 'hello' 'hello' '' '' '' '' 'hello' 'hello']
['hello' 'hello' 'hello' '' '' 'hello' '' 'hello' 'hello' '']
['' 'hello' '' 'hello' 'hello' 'hello' 'hello' '' '' '']
['' '' '' '' 'hello' '' 'hello' 'hello' 'hello' '']
['' '' 'hello' '' '' '' 'hello' 'hello' '' 'hello']]
Verifying a["x"]
验证一个[x]
>>> print a["x"]
[[ 2 -4 -10 -3 -4 4 3 -8 -10 2]
[ 5 -9 -4 -1 9 -10 3 0 -8 2]
[ 5 -4 -10 -10 -1 -8 -1 0 8 -4]
[ -7 -3 -2 4 6 6 -8 3 -8 8]
[ 1 2 2 -6 2 -9 3 6 6 -6]
[ -6 2 -8 -8 4 5 8 7 -5 -3]
[ -5 -1 -1 9 5 -7 2 -2 -9 3]
[ 3 -10 7 -8 -4 -2 -4 8 5 0]
[ 5 6 5 8 -8 5 -10 -6 -2 1]
[ 9 4 -8 6 2 4 -10 -1 9 -6]]