-3

I have two lists

one = ['allen', 'steve', 'george', 'max']
two = [88.63, 33.99, 72.66, 92.11]

I would like two lists sorted as below.

one = ['max', 'allen', 'george', 'steve']
two = [92.11, 88.63, 72.66 33.99]

What I have tried

one, two = sorted(zip(one, two))
ipj
  • 3,488
  • 1
  • 14
  • 18
Let'sbecool
  • 104
  • 6

2 Answers2

0

One way of doing that:

one, two = zip(*sorted(zip(one, two), key=lambda tup: tup[1], reverse=True))
S.B
  • 13,077
  • 10
  • 22
  • 49
0

You could combine the lists into a matrix and then sort the matrix using the numbers. The matrix is a 2D list that would group each number with a name. You can make this matrix using a list comprehension

combinedList = [[one[x], two[x]] for x in range(len(two))]

output:

[['max', 92.11], ['allen', 88.63], ['george', 72.66], ['steve', 33.99]]

Then, sort the list using the 2nd item in each entry (index 1)

outputList = sorted(combinedList, key=lambda x: x[1])

output:

[['steve', 33.99], ['george', 72.66], ['allen', 88.63], ['max', 92.11]]

For further processing, you could once again split this list back into list one and two, but sorted. Again, using list comprehension

one = [x[0] for x in outputList]
two = [x[1] for x in outputList]

output:

['steve', 'george', 'allen', 'max']
[33.99, 72.66, 88.63, 92.11]
nicholas
  • 306
  • 2
  • 9