here2share

# b_sort_list_by_list.py

Dec 25th, 2021 (edited)
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. # b_sort_list_by_list.py
  2.  
  3. XX = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' ]
  4. YY = [ '0', '1', '1', '0', '1', '2', '2', '0', '1' ] # only written as strings just to make easier to read
  5.  
  6. ZZ = [x for _,x in sorted(zip(YY,XX))]
  7. print(ZZ)
  8. print('')
  9.  
  10. #    [ 'a', 'd', 'e', 'b', 'f', 'h', 'i', 'c', 'g' ] <<< expected
  11. #                 X         X    X         X
  12. #    [ 'a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g' ] <<< result
  13.  
  14. ZZ = YY[:]
  15. for k,v in enumerate(sorted(z[::-1] for z in enumerate(YY))):
  16.     ZZ[v[1]] = XX[k]
  17.     print (ZZ)
  18. print(ZZ) # apparent solution
  19. print('')
  20. print(YY)
  21.  
  22. ['a', '1', '1', '0', '1', '2', '2', '0', '1']
  23. ['a', '1', '1', 'b', '1', '2', '2', '0', '1']
  24. ['a', '1', '1', 'b', '1', '2', '2', 'c', '1']
  25. ['a', 'd', '1', 'b', '1', '2', '2', 'c', '1']
  26. ['a', 'd', 'e', 'b', '1', '2', '2', 'c', '1']
  27. ['a', 'd', 'e', 'b', 'f', '2', '2', 'c', '1']
  28. ['a', 'd', 'e', 'b', 'f', '2', '2', 'c', 'g']
  29. ['a', 'd', 'e', 'b', 'f', 'h', '2', 'c', 'g']
  30. ['a', 'd', 'e', 'b', 'f', 'h', 'i', 'c', 'g']
  31. ['a', 'd', 'e', 'b', 'f', 'h', 'i', 'c', 'g']
  32.  
  33. ['0', '1', '1', '0', '1', '2', '2', '0', '1']
Add Comment
Please, Sign In to add comment