Python Numpy 结构化数组(recarray)将值分配给切片

发布于 2024-09-06 03:24:26 字数 649 浏览 11 评论 0原文

以下示例显示了我想要执行的操作:

>>> test
rec.array([(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
   (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)], 
  dtype=[('ifAction', '|i1'), ('ifDocu', '|i1'), ('ifComedy', '|i1')])

>>> test[['ifAction', 'ifDocu']][0]
(0, 0)

>>> test[['ifAction', 'ifDocu']][0] = (1,1)
>>> test[['ifAction', 'ifDocu']][0]
(0, 0)

因此,我想将值 (1,1) 分配给 test[['ifAction', 'ifDocu']][0]< /代码>。 (最终,我想做一些类似 test[['ifAction', 'ifDocu']][0:10] = (1,1) 的事情,为 0 分配相同的值:10 我尝试了很多方法但没有成功,

谢谢 。 俊

The following example shows what I want to do:

>>> test
rec.array([(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
   (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)], 
  dtype=[('ifAction', '|i1'), ('ifDocu', '|i1'), ('ifComedy', '|i1')])

>>> test[['ifAction', 'ifDocu']][0]
(0, 0)

>>> test[['ifAction', 'ifDocu']][0] = (1,1)
>>> test[['ifAction', 'ifDocu']][0]
(0, 0)

So, I want to assign the values (1,1) to test[['ifAction', 'ifDocu']][0]. (Eventually, I want to do something like test[['ifAction', 'ifDocu']][0:10] = (1,1), assigning the same values for for 0:10. I have tried many ways but never succeeded. Is there any way to do this?

Thank you,
Joon

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不寐倦长更 2024-09-13 03:24:26

当您说 test['ifAction'] 时,您将看到数据视图。
当您说 test[['ifAction','ifDocu']] 时,您正在使用花式索引,从而获得数据的副本。该副本对您没有帮助,因为修改副本会使原始数据保持不变。

因此,解决这个问题的方法是分别为 test['ifAction']test['ifDocu'] 赋值:

test['ifAction'][0]=1
test['ifDocu'][0]=1

例如:

import numpy as np
test=np.rec.array([(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
   (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)], 
  dtype=[('ifAction', '|i1'), ('ifDocu', '|i1'), ('ifComedy', '|i1')])

print(test[['ifAction','ifDocu']])
# [(0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0)]
test['ifAction'][0]=1
test['ifDocu'][0]=1

print(test[['ifAction','ifDocu']][0])
# (1, 1)
test['ifAction'][0:10]=1
test['ifDocu'][0:10]=1

print(test[['ifAction','ifDocu']])
# [(1, 1) (1, 1) (1, 1) (1, 1) (1, 1) (1, 1) (1, 1) (1, 1) (1, 1) (1, 1)]

为了更深入地了解底层,请参阅 Robert Kern 的这篇文章

When you say test['ifAction'] you get a view of the data.
When you say test[['ifAction','ifDocu']] you are using fancy-indexing and thus get a copy of the data. The copy doesn't help you since modifying the copy leaves the original data unchanged.

So a way around this is to assign values to test['ifAction'] and test['ifDocu'] individually:

test['ifAction'][0]=1
test['ifDocu'][0]=1

For example:

import numpy as np
test=np.rec.array([(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
   (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)], 
  dtype=[('ifAction', '|i1'), ('ifDocu', '|i1'), ('ifComedy', '|i1')])

print(test[['ifAction','ifDocu']])
# [(0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0)]
test['ifAction'][0]=1
test['ifDocu'][0]=1

print(test[['ifAction','ifDocu']][0])
# (1, 1)
test['ifAction'][0:10]=1
test['ifDocu'][0:10]=1

print(test[['ifAction','ifDocu']])
# [(1, 1) (1, 1) (1, 1) (1, 1) (1, 1) (1, 1) (1, 1) (1, 1) (1, 1) (1, 1)]

For a deeper look under the hood, see this post by Robert Kern .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文