[英]How can I find the index of a tuple inside a numpy array?
I have a numpy array as: 我有一个numpy数组:
groups=np.array([('Species1',), ('Species2', 'Species3')], dtype=object)
. groups=np.array([('Species1',), ('Species2', 'Species3')], dtype=object)
。
When I ask np.where(groups == ('Species2', 'Species3'))
or even np.where(groups == groups[1])
I get an empty reply: (array([], dtype=int64),)
当我问
np.where(groups == ('Species2', 'Species3'))
甚至np.where(groups == groups[1])
我得到一个空的回复: (array([], dtype=int64),)
Why is this and how can I get the indexes for such an element? 为什么这样,我如何获得这样一个元素的索引?
Yes you can search it but not with the np.where
but with the hep of for
loop and if-else
是的你可以搜索它,但不能搜索
np.where
但是使用for
循环和if-else
for index,var in enumerate(groups):
if var == ('Species2', 'Species3'):
print("('Species2', 'Species3') -->>", index)
else:
print("('Species1',) -->>", index)
Output 产量
('Species1',) -->> 0
('Species2', 'Species3') -->> 1
The problem here is probably the way array.__contains__()
is implemented. 这里的问题可能是
array.__contains__()
方式。 See here . 看到这里 。 Basically the issue is that
基本上问题是
print(('Species2', 'Species3') in groups)
prints False. 打印错误。 If you want to use the
numpy.where
function nonetheless, and not a for loop as the other answer suggests, it is probably best to somehow construct a suitable truth mask. 如果你想使用
numpy.where
函数,而不是另一个答案所暗示的for循环,最好以某种方式构造一个合适的真值掩码。 For example 例如
x = np.array(list(map(lambda x: x== ('Species2', 'Species3'), groups)))
print(np.where(x))
gives the correct result. 给出正确的结果。 There might be a more elegant way though.
可能会有更优雅的方式。
It's not means search a tuple('Species2', 'Species3') from groups when you use 这并不意味着在您使用时从组中搜索元组('Species2','Species3')
np.where(groups == ('Species2', 'Species3'))
it means search 'Species2' and 'Species3' separately if you have a Complete array like this 它意味着如果您有像这样的完整数组,请分别搜索'Species2'和'Species3'
groups=np.array([('Species1',''), ('Species2', 'Species3')], dtype=object)
Your array has two tuples: 你的数组有两个元组:
In [53]: groups=np.array([('Species1',), ('Species2', 'Species3')], dtype=object)
In [54]: groups
Out[54]: array([('Species1',), ('Species2', 'Species3')], dtype=object)
In [55]: groups.shape
Out[55]: (2,)
But be careful with that kind of definition. 但要小心这种定义。 If the tuples were all the same size, the array would have a different shape, and the elements would no longer be tuples.
如果元组的大小都相同,则数组将具有不同的形状,并且元素将不再是元组。
In [56]: np.array([('Species1',), ('Species2',), ('Species3',)], dtype=object)
Out[56]:
array([['Species1'],
['Species2'],
['Species3']], dtype=object)
In [57]: _.shape
Out[57]: (3, 1)
Any use of where
is only as good as the boolean array given to it. 任何使用
where
只能与给它的布尔数组一样好。 This where
returns empty because the equality test produces all False
: 这
where
因为平等的测试生产的所有返回空False
:
In [58]: np.where(groups == groups[1])
Out[58]: (array([], dtype=int64),)
In [59]: groups == groups[1]
Out[59]: array([False, False])
If I use a list comprehension to compare the group elements: 如果我使用列表推导来比较组元素:
In [60]: [g == groups[1] for g in groups]
Out[60]: [False, True]
In [61]: np.where([g == groups[1] for g in groups])
Out[61]: (array([1]),)
But for this sort of thing, a list would be just as good 但是对于这种事情,列表也同样好
In [66]: alist = [('Species1',), ('Species2', 'Species3')]
In [67]: alist.index(alist[1])
Out[67]: 1
In [68]: alist.index(('Species1',))
Out[68]: 0
In [69]: alist.index(('Species2',))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-69-0b16b56ad28c> in <module>
----> 1 alist.index(('Species2',))
ValueError: ('Species2',) is not in list
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.