我想做数组索引。我原本期望结果是 [0,1,1,0],但是我得到了一个错误。我怎样才能进行这种类型的索引?
a_np_array=np.array(['a','b','c','d'])
print a_np_array in ['b', 'c']
Traceback (most recent call last):
File "dfutmgmt_alpha_osis.py", line 130, in <module>
print a_np_array in ['b', 'c']
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
在上面,我实际上想说的是 [False,True,True,False] 而不是 [0,1,1,0],因为我想要 bool 值,这样我就可以进行索引
最佳答案
试试这个 list comprehension :
>>> print [int(x in ['b', 'c']) for x in a_np_array]
[0, 1, 1, 0]
利用int(True) == 1
这一事实和int(False) == 0
关于python 数组在列表中索引列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28814490/