Open In App

Python – Custom Pool Sorting

Last Updated : 29 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Given list and priority lists, sort the list elements on basis of their occurrence in priority lists, i.e element occurring in list1, should occur 1st and in other list should occur after that.

Input : test_list = [5, 6, 3, 7], prio1_list = [6, 3], prio2_list = [5, 7]
Output : [6, 3, 5, 7]
Explanation : 6, 3 occur in p1 list, followed by 5 and 7 which lie in p2 list.

Input : test_list = [5, 6], prio1_list = [6], prio2_list = [5]
Output : [6, 5]
Explanation : 6 occurs in 1st priority list than 5.

Method : Using sort() + comparator key function
The generic sort() can be used to perform this task. The real algorithm lies in comparator function passed in it. The assignment of appropriate return value and its order is used to solve this problem.




# Python3 code to demonstrate working of 
# Custom Pool Sorting
# Using sort() + comparator key function
  
# comparator function
def func(ele):
  
    # Returning 1 or 2 ro assign priority
    if ele in prio1_list:
        return  1
    elif ele in prio2_list:
        return  2
  
# initializing list
test_list = [5, 6, 3, 7, 4, 2, 9, 10
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing priority lists
prio1_list = [4, 6, 3, 8, 10]
prio2_list = [5, 7, 1, 2, 9]
  
# Using sort() + comparator key function
# key passed with function to manage priority
test_list.sort(key = func)
  
# printing result 
print("List after sorting : " + str(test_list))


Output :

The original list is : [5, 6, 3, 7, 4, 2, 9, 10]
List after sorting : [6, 3, 4, 10, 5, 7, 2, 9]

 


Similar Reads

Python | Custom sorting in list of tuples
Sometimes, while working with list of tuples, we can have a problem in which we need to perform it's sorting. Naive sorting is easier, but sometimes, we have to perform custom sorting, i.e by decreasing order of first element and increasing order of 2nd element. And these can also be in cases of different types of tuples. Let's discuss certain case
4 min read
Different ways of sorting Dictionary by Keys and Reverse sorting by keys
Prerequisite: Dictionaries in Python A dictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. We can access the values of the dictionary using keys. In this article, we will discuss 10 different ways of sorting the Python dictionary by keys and a
8 min read
Different ways of sorting Dictionary by Values and Reverse sorting by values
Prerequisite: Dictionaries in Python A dictionary is a collection which is unordered, changeable, and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. We can access the values of the dictionary using keys. In this article, 10 different ways of sorting the Python dictionary by values and also reverse s
15+ min read
Python Program for Topological Sorting
Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge uv, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG. Python Program for Topological SortingFor example, a topological sorting of the following graph is "5 4 2 3 1
4 min read
Python | Integrity Sorting in two lists
Often during the problem solving we come across too many problems where we need to sort the list. But sometimes we would also want to sort the another list so that the elements of are automatically shifted and remain at same index as the first list even after first list get sorted. Let's discuss certain ways in which this can be done. Method #1 : U
4 min read
Python | Sorting list of lists with similar list elements
Sorting has always been a key operation that is performed for many applications and also as a subproblem to many problems. Many variations and techniques have been discussed and their knowledge can be useful to have while programming. This article discusses the sorting of lists containing a list. Let's discuss certain ways in which this can be perf
5 min read
Python | Sorting URL on basis of Top Level Domain
Given a list of URL, the task is to sort the URL in the list based on the top-level domain. A top-level domain (TLD) is one of the domains at the highest level in the hierarchical Domain Name System of the Internet. Example - org, com, edu. This is mostly used in a case where we have to scrap the pages and sort URL according to top-level domain. It
3 min read
Python | Sorting string using order defined by another string
Given two strings (of lowercase letters), a pattern and a string. The task is to sort string according to the order defined by pattern and return the reverse of it. It may be assumed that pattern has all characters of the string and all characters in pattern appear only once. Examples: Input : pat = "asbcklfdmegnot", str = "eksge" Output : str = "g
2 min read
Python | Inverse Sorting String
Sometimes, while participating in a competitive programming test, we can be encountered with a problem in which we require to sort a pair in opposite orders by indices. This particular article focuses on solving a problem in which we require to sort the number in descending order and then the String in increasing order. This is the type of problem
3 min read
Python - Synchronized Sorting of Keys
Sometimes, while working with Python Dictionaries, we can have a problem in which we need to perform the sort of one key of dictionary and which to perform similar changes to corresponding keys as well. This kind has application in web development and competitive programming. Let's discuss certain way in which this task can be performed. Input : te
3 min read
Practice Tags :