I have a list 'distancechunks' which has 90 lists inside it. Each of these 90 lists consist of 2 tuples inside them (see image). I want to sort every list (90 total) by the first tuple (yellow highlighted in image). So as a result every list is ordered low to high. The array value (2nd tuple) should not be sorted, just maintained with the same value on the left.
I tried the classic things like .sort() and sorting with lambda etc:
for x in distancechunks:
for i in x:
lst2 = sorted(i)
print(lst2)
However, I always get different error messages like:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
or I tried:
vallah = distancechunks
vallah.sort(key=lambda x: int(x[0]))
vallah
and get the error message:
int() argument must be a string, a bytes-like object or a number, not 'tuple'
Unfortunately, I am not sure how to implement this error...
Here is a excerp of the list 'distancechunks':
[[(0.6164414002968976, array([6.2, 2.8, 4.8, 1.8])),
(1.5132745950421556, array([5.7, 2.8, 4.1, 1.3])),
(0.648074069840786, array([6. , 3. , 4.8, 1.8])),
(0.8124038404635961, array([6.5, 2.8, 4.6, 1.5])),
(0.6480740698407862, array([6.3, 2.9, 5.6, 1.8])),
(4.497777228809803, array([4.8, 3. , 1.4, 0.1])),
(0.5291502622129182, array([6.1, 3. , 4.9, 1.8])),
(1.1445523142259595, array([6.2, 2.9, 4.3, 1.3])),
(0.4242640687119287, array([6.7, 3. , 5.2, 2.3])),
(1.4866068747318506, array([7.3, 2.9, 6.3, 1.8])),
(0.6480740698407865, array([6.7, 3.3, 5.7, 2.1])),
(2.135415650406262, array([5.7, 2.6, 3.5, 1. ])),
(0.7416198487095664, array([6.3, 2.5, 5. , 1.9])),
(0.6782329983125268, array([6.7, 3.1, 5.6, 2.4])),
(0.9055385138137417, array([6.3, 2.5, 4.9, 1.5])),
(4.135214625627066, array([5. , 3.4, 1.6, 0.4])),
(4.050925820105819, array([4.8, 3.4, 1.9, 0.2])),
(4.451965857910412, array([4.6, 3.1, 1.5, 0.2])),
(1.6792855623746663, array([5.8, 2.7, 3.9, 1.2])),
(1.4071247279470291, array([7.4, 2.8, 6.1, 1.9])),
(0.6999999999999996, array([5.9, 3.2, 4.8, 1.8])),
(0.7549834435270752, array([6.5, 3. , 5.8, 2.2])),
(4.22965719651132, array([5.3, 3.7, 1.5, 0.2])),
(4.3, array([5. , 3.4, 1.5, 0.2])),
(4.3977266854592045, array([5. , 3.6, 1.4, 0.2])),
(0.8366600265340757, array([6.8, 2.8, 4.8, 1.4])),
(0.9055385138137422, array([6.8, 3.2, 5.9, 2.3])),
(0.46904157598234314, array([6.4, 3.1, 5.5, 1.8])),
(4.266145801540308, array([5.1, 3.4, 1.5, 0.2])),
(1.8734993995195195, array([5.5, 2.3, 4. , 1.3]))],
[(1.1575836902790229, array([7.2, 3.2, 6. , 1.8])),
(2.692582403567252, array([5. , 2. , 3.5, 1. ])),
(2.0976176963403037, array([7.7, 3.8, 6.7, 2.2])),
(3.972404813208241, array([5.1, 3.3, 1.7, 0.5])),
(0.4242640687119286, array([6.7, 3. , 5. , 1.7])),
(4.262628297189423, array([5.1, 3.8, 1.5, 0.3])),
[sorted(lst) for lst in distancechunks]
should work just fine. Maybe thearray
things are not comparable (default sort would use them as tie-breakers in case of equal first value); in this case try[sorted(lst, key=lambda t: t[0]) for lst in distancechunks]
– tobias_k