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]
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.
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.
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.
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.
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.
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.
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:
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]
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With