Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python sorting two lists

I am trying to sort two lists together:

list1 = [1, 2, 5, 4, 4, 3, 6] list2 = [3, 2, 1, 2, 1, 7, 8]  list1, list2 = (list(x) for x in zip(*sorted(zip(list1, list2)))) 

Anyway, doing this gives me on output

list1 = [1, 2, 3, 4, 4, 5, 6] list2 = [3, 2, 7, 1, 2, 1, 8] 

while I would want to keep the initial order for equal number 4 in the first list: what I want is

list1 = [1, 2, 3, 4, 4, 5, 6] list2 = [3, 2, 7, 2, 1, 1, 8] 

What do I have to do? I wouldn't want to use loop for bubble-sorting. Any help appreciated.

like image 830
fmonegaglia Avatar asked Dec 02 '12 10:12

fmonegaglia


People also ask

How do you sort a list by another list in Python?

Use zip() to sort a list based on another list. Call zip(*iterables) on the list to sort by and the list to sort, in that order. Call sorted(iterable) on the constructed zip object to create a sorted list of tuples representing pairings between the two lists.

How do you sort two lists without sorting in Python?

You can use Nested for loop with if statement to get the sort a list in Python without sort function. This is not the only way to do it, you can use your own logic to get it done.

How do you sort 3 lists in Python?

sort() Syntax The syntax of the sort() method is: list.sort(key=..., reverse=...) Alternatively, you can also use Python's built-in sorted() function for the same purpose.


2 Answers

Use a key parameter for your sort that only compares the first element of the pair. Since Python's sort is stable, this guarantees that the order of the second elements will remain the same when the first elements are equal.

>>> from operator import itemgetter >>> [list(x) for x in zip(*sorted(zip(list1, list2), key=itemgetter(0)))] [[1, 2, 3, 4, 4, 5, 6], [3, 2, 7, 2, 1, 1, 8]] 

Which is equivalent to:

>>> [list(x) for x in zip(*sorted(zip(list1, list2), key=lambda pair: pair[0]))] [[1, 2, 3, 4, 4, 5, 6], [3, 2, 7, 2, 1, 1, 8]] 
like image 55
interjay Avatar answered Sep 20 '22 15:09

interjay


The trick here is that when Python does tuple comparisons, it compares the elements in order from left to right (for example, (4, 1) < (4, 2), which is the reason that you don't get the ordering you want in your particular case). That means you need to pass in a key argument to the sorted function that tells it to only use the first element of the pair tuple as its sort expression, rather than the entire tuple.

This is guaranteed to retain the ordering you want because:

sorts are guaranteed to be stable. That means that when multiple records have the same key, their original order is preserved.

(source)

>>> list1 = [1, 2, 5, 4, 4, 3, 6] >>> list2 = [3, 2, 1, 2, 1, 7, 8] >>>  >>> list1, list2 = (list(x) for x in zip(*sorted(zip(list1, list2), key=lambda pair: pair[0]))) >>>  >>> print list1 [1, 2, 3, 4, 4, 5, 6] >>> print list2 [3, 2, 7, 2, 1, 1, 8] 
like image 40
Mark Amery Avatar answered Sep 24 '22 15:09

Mark Amery