2

I have list

value =[(22, 11, 195, 37), (19, 11, 184, 45), (300, 17, 12, 19), (210, 18, 128, 30) (100, 18, 128, 30)]

Now i Need to sort the list in combination of first two values in the list of ascending order.

like this

s = [(19, 11, 184, 45), (22, 11, 195, 37), (300, 17, 12, 19),(100, 18, 128, 30), (210, 18, 128, 30) ]

I used

s = sorted(value, key = lambda x: (x[1], x[0]))

And i ended up sorting the list for 2nd (x[1]) value of the list like this,

s = [(22, 11, 195, 37), (19, 11, 184, 45), (300, 17, 12, 19), (210, 18, 128, 30) (100, 18, 128, 30)]

Are there any methods? I have also tried with operator library, it gives me the same result.

2
  • sorted(value, key = lambda x: (x[1], x[0])) returns the desired result not the list that starts with (22, 11, 195, 37). Commented Sep 5, 2017 at 14:15
  • Possible duplicate of Sort a list of tuples depending on two elements Commented Sep 5, 2017 at 14:41

2 Answers 2

2

As you understood, the sorted function (and the list.sort method as well) takes a key as optional argument, which tells how to sort the elements.

You're using lambda x: (x[0], x[1]) as key, but your output clearly shows that you want the second member to have more priority than the first. Try with this key:

l = sorted(l, key=lambda t: (t[1], t[0]))

This will give a higher importance to the second member of each tuple.

As expected, the output is:

[(0, 0, 559, 225),
(19, 11, 184, 45),
(22, 11, 195, 37),
(300, 17, 12, 19),
(210, 18, 128, 30)]
Sign up to request clarification or add additional context in comments.

6 Comments

Side-note: Using operator.itemgetter can be a bit more self-documenting here (and a titch faster as a bonus), e.g. key=itemgetter(1, 0).
@ShadowRanger That's smart. I did not know of itemgetter.
I used the same s = sorted(calculation, key = lambda x: (x[1], x[0])) And i still not getting sorted for my original list. which i have commented below
[(0, 0, 559, 225), (344, 11, 195, 37), (19, 11, 184, 45), (209, 18, 128, 30), (517, 20, 12, 19), (492, 20, 16, 5), (354, 20, 9, 4), (476, 21, 7, 15), (435, 21, 21, 17), (404, 21, 15, 17), (30, 21, 15, 17), (305, 28, 8, 10), (242, 28, 2, 1), (128, 28, 7, 10), (83, 28, 13, 10), (61, 28, 12, 10), (354, 32, 11, 7), (492, 33, 16, 6), (285, 37, 1, 1), (242, 37, 3, 1), (221, 37, 1, 1), (152, 37, 2, 1), (291, 64, 138, 46), (132, 65, 154, 37), (142, 74, 10, 5), (403, 81, 3, 11), (379, 82, 15, 10), (360, 82, 2, 1)] This is a part of my list
@DeepanRaj I think that the problem is that your criterion is not well-defined. What you asked is rather unclear, but the solution I provided you does work for the sample list you gave. Could you try and define more precisely what behaviour you're expecting?
|
1

Okay after a lot of fiddleing i think I did it. Here is my code:

sorted(s, key=lambda x: (x[1] * 1, x[0]))

output is:

[(19, 11, 184, 45), (22, 11, 195, 37), (300, 17, 12, 19), (100, 18, 128, 30), (210, 18, 128, 30)]

Basically what this does is sorts the tuples by index 1 but if two or more tuples have the same number on index 1 it checks their index 0 numbers and sorts them ascendingly.

11 Comments

Actually the list was big, so i made my own list, here is the full list [(0, 0, 559, 225), (344, 11, 195, 37), (19, 11, 184, 45), (209, 18, 128, 30), (517, 20, 12, 19), (492, 20, 16, 5), (354, 20, 9, 4), (476, 21, 7, 15), (435, 21, 21, 17), (404, 21, 15, 17), (30, 21, 15, 17), (305, 28, 8, 10), (242, 28, 2, 1), (128, 28, 7, 10), (83, 28, 13, 10), (61, 28, 12, 10), (354, 32, 11, 7), (492, 33, 16, 6), (285, 37, 1, 1), (242, 37, 3, 1), (221, 37, 1, 1), (152, 37, 2, 1), (291, 64, 138, 46), (132, 65, 154, 37), (142, 74, 10, 5), (403, 81, 3, 11), (379, 82, 15, 10), (360, 82, 2, 1)]
Even this is a part of list. I want to sort for first two values from the list
@DeepanRaj yeah but how do you want it to be sorted? What do you mean by first two values. The question is unclear...
The first two values in the list say [(0,0,559,225)] in this the first two values 0 and 0, based on the ascending order combination of these two values i need to sort the list
your question is in contradiction with the example you provided
|

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.