list,python-2.7,sorting , Sort List of Numbers according to Custom Number Sequence

## Question:

Tag: list,python-2.7,sorting

Question :
A set of numbers will be passed as input. Also the redefined relationship of the digits 0-9 in ascending order will be passed as input. Based on the redefined relationship, the set of numbers must be listed in ascending order.

Input Format:
The first line will contain the the set of numbers. The next line will contain the digits 0-9 in the redefined ascending order.

Boundary Conditions:
The size of the set of numbers will be from 2 to 100.

Output Format:
The set of numbers in ascending order as per the redefined order of digits separated by a space.

Example Input/Output 1:

Input:

``````20 50 11 121
9231476058
``````

Output:

``````50 11 20 121
``````

Explanation:

``````121 is a three digit number and hence comes first.
As per the redefined order 2 > 1 > 5.
So 121 is greater than all others and comes in the end.
20 > 11 > 50 and hence in ascending order this is reversed.
``````

Example Input/Output 2:

Input:

``````319 311 198 420
1948327605
``````

Output:

``````319 311 420 198
``````

Explanation:

``````As per the redefined order 1 > 4 > 3
Among  319 and 311, 1 > 9
Hence the final ascending order is 319 311 420 198
``````

My Solution :

``````if __name__ == '__main__':
list_ = raw_input().split()
num = str(raw_input())
output = sorted(list_, key = num.index)
print(' '.join(output))
``````

I need to know how to do multiple levels of sorting such that it compares indexes of first character, then second character & so on...

This matches your input/output examples, but I had to use descending numbers to get the example answers. Are you sure your explanation is correct? If not, just use `0123456789` instead of `9876543210` in the code below.

The algorithm is to provide a sorting key based on translating the digits of the number into their corresponding rank digits:

``````import string

def xsort(L,xlat):
def xform(s):
return int(str(s).translate(string.maketrans(xlat,'9876543210')))
return sorted(L,key=xform)

print xsort([20,50,11,121],'9231476058')
print xsort([319,311,198,420],'1948327605')
``````

Output:

``````[50, 11, 20, 121]
[319, 311, 420, 198]
``````

References: str.translate, string.maketrans

# Related:

## multiple iteration of the same list

python,python-2.7,python-3.x,numpy,shapely
I have one list of data as follows: from shapely.geometry import box data = [box(1,2,3,4), box(4,5,6,7), box(1,2,3,4)] sublists = [A,B,C] The list 'data' has following sub-lists: A = box(1,2,3,4) B = box(4,5,6,7) C = box(1,2,3,4) I have to check if sub-lists intersect. If intersect they should put in one tuple;...

## Slicing a Python OrderedDict

python-2.7,slice,ordereddictionary
In my code I frequently need to take a subset range of keys+values from a Python OrderedDict (from collections package). Slicing doesn't work (throws TypeError: unhashable type) and the alternative, iterating, is cumbersome: from collections import OrderedDict o = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4)]) # want to...

## How to check for multiple attributes in a list

python,python-2.7
I am making a TBRPG game using Python 2.7, and i'm currently making a quest system. I wanted to make a function that checks all of the quests in a list, in this case (quests), and tells you if any of of the quests in the list have the same...

## represent an index inside a list as x,y in python

python,list,numpy,multidimensional-array
I have a list which contains 1000 integers. The 1000 integers represent 20X50 elements of dimensional array which I read from a file into the list. I need to walk through the list with an indicator in order to find close elements to each other. I want that my indicator...

## Python initialize strings as variable

python,python-2.7
I have a calculation schema as string calc = "((k+m+46)/2)" and some strings containing variable like m = 2 k = m*2 all only strings. Now I want to initialize them into Python. my goal is it to calculate with the calculating schema the varible values. calc should return 26...