2

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:

  1. value of val1 (highest first)
  2. 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.

11
  • 1
    Could you clarify the link between (x,y,z) and [[val_1, nb_1],[val_2,nb_2],...]]` ? Commented Aug 20, 2012 at 14:27
  • 4
    yourList.sort(key=lambda x: x[1][0], reverse=True) Commented Aug 20, 2012 at 14:27
  • 2
    @RickyRobinson: x[1][0] gives you both items [val1, occurrences_of_val1], which will sort the way you requested. Commented Aug 20, 2012 at 14:35
  • 1
    @mgilson: That was not originally specified, but this would do it: yourList.sort(key=lambda x: x[1], reverse=True) Commented Aug 20, 2012 at 14:39
  • 1
    @RickyRobinson: Then don't narrow down to x[1][0], just leave the key as x[1]. Commented Aug 20, 2012 at 14:41

2 Answers 2

7

yourList.sort(key=lambda x: x[1], reverse=True)

Sign up to request clarification or add additional context in comments.

8 Comments

I was just about to suggest you post this as an answer.
Do we know what's going on with the fact val1 is a string but none of the others are?
@DSM: I noticed that. It will cause issues if the values are not between '0.0' and '9.9'. There is some datatype strangeness. Also, I think the OP might be better off with a list of objects.
I just realised val1 is indeed a string, but it seems that values (they are included in [0,1]) get sorted in the expected way.
It's ok then, val_i is in [0,1] for each i
|
3

I tested this

X=[ [[4,5,6], [[3.0, 4], [0.8, 3], [0.5, 10], [0.1, 11]]],
  [[2,1,3], [[2.0, 8], [0.8, 3], [0.2, 1], [0.1, 8]]]]
>>> X.sort(key=lambda x: x[1])
>>> X
[[[2, 1, 3], [[2.0, 8], [0.80000000000000004, 3], [0.20000000000000001, 1], [0.10000000000000001, 8]]], [[4, 5, 6], [[3.0, 4], [0.80000000000000004, 3], [0.5, 10], [0.10000000000000001, 11]]]]

Most importantly, X.sort(key=lambda x: x[1]) is sorting by second element of X, that is by this elements [[val1, occurrences_of_val1], [val2, occurrences_of_val2],...]. Implicitely, it is sorting by val1, then in case of equality by occurrences_of_val1 ...

1 Comment

Can you explain how this line works: X.sort(key=lambda x: x[1])? It works but I don't quite understand it. Isn't it we need to pass in a function as an argument to the list.sort([fun])?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.