0

I have an array g=[9,7,8,3,4] and I want to sort it (descending) based on f=[12, 3, 5,45, 2] so I want a sorted array of g as m=[3,9,8,7,4]. My code is wrong but here it is:

f=[12, 3, 5,45, 2]

g=[9,7,8,3,4]

m= sorted(g, key = lambda temp:f)
print(m)
ELLA
  • 45
  • 4
  • Why do you have separate but corresponding lists in the first place, how did they come to be? Did you maybe build `f` from `g`, so that you could've used the same to instead sort? Or to build pairs so that what belongs together *is* together? – Kelly Bundy Sep 26 '22 at 16:50

1 Answers1

1

Something like this ?

f = [12, 3, 5, 45, 2]
g = [9, 7, 8, 3, 4]
m = list(x for _, x in sorted(zip(f, g), reverse=True))
print(m) # [3, 9, 8, 7, 4]
ErnestBidouille
  • 1,071
  • 4
  • 16