Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort one list based on another? [duplicate]

I have two list, one reference and one input list

Ref = [3, 2, 1, 12, 11, 10, 9, 8, 7, 6, 5, 4]
Input = [9, 5, 2, 3, 10, 4, 11, 8]

I want to sort Input list, in the order as that of Ref. If some element is missing in Input list, it can skip and go for the other element.

Hence sorted Input list, based on Ref list will be like this

Sorted_Input = [3, 2, 11, 10, 9, 8, 5, 4]
like image 372
Hardik Gupta Avatar asked Dec 25 '19 16:12

Hardik Gupta


People also ask

How to sort list in Python with sort function?

The easiest way to sort is with the sorted(list) function, which takes a list and returns a new list with those elements in sorted order. The original list is not changed. It's most common to pass a list into the sorted() function, but in fact it can take as input any sort of iterable collection.

How do I reorder a list from another list in Python?

Use the zip() and sorted() Functions to Sort the List Based on Another List in Python. In this method, we will use the zip() function to create a third object by combining the two given lists, the first which has to be sorted and the second on which the sorting depends.

How order a list in Python?

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.

How to sort a list based on values from another list?

We will learn how to sort a given list based on the values from another list in this article. In this method, we will use the zip () function to create a third object by combining the two given lists, the first which has to be sorted and the second on which the sorting depends.

How do you sort a zip list in Python?

Create a new, sorted list based on the zip using sorted (). Using a list comprehension extract the first elements of each pair from the sorted, zipped list. The purpose of zip () is to map a similar index of multiple containers so that they can be used just using as a single entity.

How to sort data based on first cell value in Excel?

Tip: C:C is the column you want to resort based on, and A1 is the first cell of the column you want to sort. 2. And then a list of numbers are displaying in the formula cells. Select the list you want to resort and the numbers list, and click Data > Sort. See screenshot: 3.

How to sort data from drop down list by value in Excel?

Select the list you want to resort and the numbers list, and click Data > Sort. See screenshot: 3. And in the Sort dialog, select the column which contains the formulas from Column drop down list, and select Values and Smallest to Largest from the Sort On and Order drop down lists separately. See screenshot:


3 Answers

I think this answers your question:

>>> [x for x in Ref if x in Input]
>>> [3, 2, 11, 10, 9, 8, 5, 4]

Hope it helps.

UPDATE: Making Input a set for faster access:

>>> Input_Set = set(Input)
>>> [x for x in Ref if x in Input_Set]
[3, 2, 11, 10, 9, 8, 5, 4]
like image 142
dcg Avatar answered Oct 19 '22 01:10

dcg


Another approach in addition to dcg's answer would be as follows:

Ref = [3, 2, 1, 12, 11, 10, 9, 8, 7, 6, 5, 4]
Input = [9, 5, 2, 3, 10, 4, 11, 8]

ref = set(Ref)
inp = set(Input)

sorted_list = sorted(ref.intersection(inp), key = Ref.index)

This outputs to:

[3, 2, 11, 10, 9, 8, 5, 4]

Here you convert the lists into sets, find their intersection, and sort them. The set is sorted based on the 'Ref' list's indexing.

like image 36
A. Dhakal Avatar answered Oct 18 '22 23:10

A. Dhakal


You can use the sorted method:

# keep in a dict the index for each value from Ref
ref  = {val: i for i, val in enumerate(Ref)}
# sort by the index value from Ref for each number from Input 
sorted(Input, key=ref.get)

output:

[3, 2, 11, 10, 9, 8, 5, 4]
like image 8
kederrac Avatar answered Oct 18 '22 23:10

kederrac