Jonathan
0
Q:

how to sort a list according to another list in python

# Python program to sort  
# one list using 
# the other list 
  
def sort_list(list1, list2): 
  
    zipped_pairs = zip(list2, list1) 
  
    z = [x for _, x in sorted(zipped_pairs)] 
      
    return z 
      
  
# driver code 
x = ["a", "b", "c", "d", "e", "f", "g", "h", "i"] 
y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1] 
  
print(sort_list(x, y)) 
  
x = ["g", "e", "e", "k", "s", "f", "o", "r", "g", "e", "e", "k", "s"] 
y = [ 0,   1,   1,    0,   1,   2,   2,   0,   1] 
  
print(sort_list(x, y)) 
0

list1 = [[1,1],[1,2],[1],[2,2]]
list2 = [2, 3, 1, 4]

zipped_lists = zip(list1, list2)
sorted_pairs = sorted(zipped_lists)

tuples = zip(*sorted_pairs)
c,v = [ list(tuple) for tuple in  tuples]
print(c)
0

New to Communities?

Join the community