我有以下代码:
# unicorns is a numpy array with several fields
idx = (1, 2, 3, 5, 7)
unicorns=uni[idx]
# now i have only the first, second, third, ... unicorn
print unicorns
但是如果我想再选择这个独角兽阵列
^{pr2}$这应该能告诉我白色独角兽的名字,纽比只解释
color==white
部分为False
,它转到0并返回数组的第一个条目。在
我怎么能修正这个代码,让它做我想要它做的,选择白色独角兽?在
我希望所有的东西都保持numpy,这样我也可以选择独角兽的其他属性。在
编辑
以下是阵列的示例:
unicorns=[(1, black, 0.0, 'Pinky', 1) (2, black, 0.0, 'Winky', 1)
(3, white, 0.0, 'Lala', 1) (4, white, 0.0, 'Merlin', 1)
(5, black, 0.0, 'Meriva', 1) (6, white, 0.0, 'Panda', 1)]
idx = [ 0 , 3 , 6 ]
您可能想要使用的是^{} 函数。
这样使用:
您还可以看看pandas,它对于此类切片和查询操作来说非常棒。你的问题可以这样解决:
我不得不稍微修改一下数组,使之成为有效的Python代码。如果我正确地转换了它,那么我想你要找的是:
^{pr2}$
相关问题 更多 >
编程相关推荐