numpy where with array of tuples

主宰稳场 提交于 2021-02-05 06:48:25

问题


Why can't I find the location of a tuple in an array? Afterall, the bottom expression prints True

foo = numpy.array([(5, 30), (5,), 5])
bar = numpy.where(foo==foo[0])
print(bar)

Prints (array([], dtype=int64),)

print((5,30)==foo[0])

Prints True


回答1:


It's because:

import numpy

foo = numpy.array([(5, 30), (5,), 5])
bar = numpy.where(foo==foo[0])
print(foo==foo[0])

False

That's why you're getting an empty array. A list comprehension alternative is [v for v in foo if v == foo[0]] will result in [(5, 30)]



来源:https://stackoverflow.com/questions/33609871/numpy-where-with-array-of-tuples

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!