I've been using this structure to sort vectors (the arrays) by some property of the vector. This structure (sorting vectors by a zipping them with scalars,and sorting by the scalars) has been working in other parts of my code, but in this case it fails with the warning: The truth value of an array with more than one element is ambiguous. This depends on there being duplicate values in the scalars (see below)
from numpy import array
pnts =[array([ 0. , 0.45402743, -0.64209154]),
array([-0.27803373, 0.45402743, -0.64209154]),
array([-0.64874546, 0.45402743, 0. ]),
array([-0.27803373, 0.45402743, 0.64209154]),
array([ 0. , 0.45402743, 0.64209154]),
array([ 0. , -0.45402743, 0.64209154]),
array([-0.27803373, -0.45402743, 0.64209154]),
array([-0.64874546, -0.45402743, 0. ]),
array([-0.27803373, -0.45402743, -0.64209154]),
array([ 0. , -0.45402743, -0.64209154]),
array([-0.46338972, 0. , 0.64209154]),
array([-0.46338972, 0. , -0.64209154]),
array([-0.83410135, 0. , 0. ])]
ds = [0.64209154071986396, 0.69970301064027385, 0.64874545642786008,
0.69970301064027385, 0.64209154071986396, 0.64209154071986396,
0.69970301064027385, 0.64874545642785986, 0.69970301064027385,
0.64209154071986396, 0.79184062463701899, 0.79184062463701899,
0.83410134835400274]
pnts = [pnt for (d,pnt) in sorted(zip(ds,pnts))] #sort by distances ds
print pnts
However if I shorten it to the first 3 points, it does work:
from numpy import array
pnts =[array([ 0. , 0.45402743, -0.64209154]),
array([-0.27803373, 0.45402743, -0.64209154]),
array([-0.64874546, 0.45402743, 0. ])]
ds = [0.64209154071986396, 0.69970301064027385, 0.64874545642786008]
pnts = [pnt for (d,pnt) in sorted(zip(ds,pnts))]
print pnts
>[array([ 0. , 0.45402743, -0.64209154]), array([-0.64874546, 0.45402743, 0. ]), array([-0.27803373, 0.45402743, -0.64209154])]
I'm sure the issue is because there are duplicates among the ds. When I go from 3 to 4 points where the first duplicate appears, it fails again. But other sorting routines in python work fine when there are duplicates. Why not this one?
listofnp.arrays.ValueErroris produced when you try to compare 2 arrays and use the result as a scalar boolean. The classic case isif A==B:. Here it's because thesortis trying to compare two arrays from theptslist.