0

I have an array called points that has the shape (408046, 2) (408046 pairs of x,y) and an array called pointsValue that has the shape (408046,) (the values at the 408046 pairs of x,y). I am trying to sort points based on its value from pointsValue. I have tried using the method explained another StackOverflow question (Sorting list based on values from another list?) using the below syntax

pointSorted = [x for _,x in sorted(zip(points,pointsValue),reverse=True)]

However, the above gives me an error saying "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"

Thanks.

Mc Missile
  • 715
  • 11
  • 25
  • The `numpy` way of doing this is using `argsort`: `idx = pointsValue.argsort()` `pointsSorted = points[idx]` – Paul Panzer Oct 16 '19 at 04:24
  • Per the other question, the source of "key" values (in your case, `pointsValue`) should come *first* in the `zip` arguments. This is because of how the trick actually works: it makes (key, value) pairs and then sorts those tuples, and the *tuple's* sort order is defined to compare by each element in order. But *you don't want to do this anyway when working with Numpy*, because the list comprehension will give you back a plain Python list instead of a Numpy array - losing all the performance advantages. – Karl Knechtel Oct 16 '19 at 04:46

0 Answers0