3

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

2
  • 1
    foo is a dtype=object array (different size tuples). Many of the usual numeric array operations, including comparison, are not implemented for this dtype. foo should be a list. Commented Nov 9, 2015 at 18:58
  • foo = numpy.array([(5, 30), (5,), 5]) ----> NOWADAYS ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part. Commented Feb 13, 2024 at 23:10

1 Answer 1

2

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)]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.