I am trying to sort a list of numpy arrays with respect to a list of integers in ascending order, the problem discussed in this post. Specifically, I am using the top rated solution from the post.
This first example produces the intended solution:
>>> x1 = [np.array([1,2,3]),np.array([4,5,6]),np.array([7,8,9])]
>>> y1 = [6, 10 , 4]
>>> y1_sorted, x1_sorted = zip(*sorted(zip(y1, x1)))
>>> y1_sorted, x1_sorted
((4, 6, 10), (array([7, 8, 9]), array([1, 2, 3]), array([4, 5, 6])))
However, this second example, with variables seemingly of the same type, produces this error:
>>> x2 = [np.array([1, 2, 3]),
... np.array([1, 3, 2]),
... np.array([2, 1, 3]),
... np.array([2, 3, 1]),
... np.array([3, 1, 2]),
... np.array([3, 2, 1])]
>>> y2 = [6,3,7,1,3,8]
>>> y2_sorted, x2_sorted = zip(*sorted(zip(y2, x2)))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Would anyone be able to explain what is happening? I am using numpy 1.20.3 with Python 3.8.12.