Python – Custom Pool Sorting
Last Updated :
29 Aug, 2020
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.
def func(ele):
if ele in prio1_list:
return 1
elif ele in prio2_list:
return 2
test_list = [ 5 , 6 , 3 , 7 , 4 , 2 , 9 , 10 ]
print ( "The original list is : " + str (test_list))
prio1_list = [ 4 , 6 , 3 , 8 , 10 ]
prio2_list = [ 5 , 7 , 1 , 2 , 9 ]
test_list.sort(key = func)
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]
Please Login to comment...