Python NumPy-基于结构化数组中的其他值设置结构化数组中的值

Python NumPy-基于结构化数组中的其他值设置结构化数组中的值,python,numpy,Python,Numpy,我有一个结构化的NumPy数组: a = numpy.zeros((10, 10), dtype=[ ("x", int), ("y", str)]) 如果a[“y”]中的相应值为负值,我想将a[“y”]中的值设置为“hello”。据我所知,我应该这样做: a["y"][a["x"] < 0] = "hello" a[“y”][a[“x”]

我有一个结构化的NumPy数组:

a = numpy.zeros((10, 10), dtype=[
    ("x", int),
    ("y", str)])
如果
a[“y”]
中的相应值为负值,我想将
a[“y”]
中的值设置为
“hello”
。据我所知,我应该这样做:

a["y"][a["x"] < 0] = "hello"
a[“y”][a[“x”]<0]=“你好”

但这似乎改变了
a[“x”]
中的值!我所做的有什么问题,我还应该怎么做?

首先,在numpy结构化数组中,当您将数据类型指定为
str
numpy
时,假定它是一个1个字符的字符串

>>> a = numpy.zeros((10, 10), dtype=[
        ("x", int), 
        ("y", str)])

>>> print a.dtype
dtype([('x', '<i8'), ('y', 'S')])
因此,使用数据类型为
a10
,其中10是字符串的最大长度

请参阅链接,它为其他数据结构指定了更多定义

其次,我认为你的方法是正确的

初始化数据类型为int和
a10的结构化numpy数组

>>> a = numpy.zeros((10, 10), dtype=[("x", int), ("y", 'a10')])
用随机数填充它

>>> 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]]
应用你的过滤

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

啊,关于数据类型的评论非常有用!谢谢至于
a[“x”]
的突变,可能与此有关?这些值都变为小写字母ASCII值内的值,所以可能是某种奇怪的溢出?我会尝试改变类型,看看会发生什么。是的,这似乎解决了我的问题!谢谢您使用的是哪一版本的numpy(
打印numpy.\uuuuu version\uuuuu
)?我看不到整数字段中有字符溢出,但那将是一个严重的错误。1.8.1,是的,是的。
a=numpy.zeros((10,10),dtype=[(((x),int),((y,str)];a[“x”][:]=numpy.random.randint(-10,10,(10,10));打印一个[“x”];a[“y”][a[“x”]<0]=“你好”;打印a[“x”]
-生成两种不同的打印输出。将
str
更改为
“a”
也会复制它。我已提交了一份错误报告,请参阅,问题似乎是创建的字符串字段长度为0。
>>> 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']]
>>> 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]]