Python Numpy 结构化数组(recarray)将值分配给切片
以下示例显示了我想要执行的操作:
>>> 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您说
test['ifAction']
时,您将看到数据视图。当您说
test[['ifAction','ifDocu']]
时,您正在使用花式索引,从而获得数据的副本。该副本对您没有帮助,因为修改副本会使原始数据保持不变。因此,解决这个问题的方法是分别为
test['ifAction']
和test['ifDocu']
赋值:例如:
为了更深入地了解底层,请参阅 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']
andtest['ifDocu']
individually:For example:
For a deeper look under the hood, see this post by Robert Kern .