In Python, I have a nested list like the following:
[ [[x1,y1,z1], [['0.9', 4], [0.8, 3], [0.5, 10], [0.1, 11]],
[[x2,y2,z2], [['1.0', 8], [0.8, 3], [0.2, 1], [0.1, 8]]
...]
So each element is in the form:
[[3-tuple], [[val1, occurrences_of_val1], [val2, occurrences_of_val2],...]]
The second nested list is already sorted by the first item (val1 > val2 > val3 ...), but I want also the 3-tuples [x,y,z] to appear sorted in descending order according to two criteria:
- value of val1 (highest first)
- in case of same val1, highest occurrences_of_val1 (possibly applied to val2 if the two values above are the same)
How do I do this? Probably with itemgetter, but I'm not sure in this case.
(x,y,z)and [[val_1, nb_1],[val_2,nb_2],...]]` ?yourList.sort(key=lambda x: x[1][0], reverse=True)x[1][0]gives you both items[val1, occurrences_of_val1], which will sort the way you requested.yourList.sort(key=lambda x: x[1], reverse=True)x[1][0], just leave the key asx[1].