Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # b_sort_list_by_list.py
- XX = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' ]
- YY = [ '0', '1', '1', '0', '1', '2', '2', '0', '1' ] # only written as strings just to make easier to read
- ZZ = [x for _,x in sorted(zip(YY,XX))]
- print(ZZ)
- print('')
- # [ 'a', 'd', 'e', 'b', 'f', 'h', 'i', 'c', 'g' ] <<< expected
- # X X X X
- # [ 'a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g' ] <<< result
- ZZ = YY[:]
- for k,v in enumerate(sorted(z[::-1] for z in enumerate(YY))):
- ZZ[v[1]] = XX[k]
- print (ZZ)
- print(ZZ) # apparent solution
- print('')
- print(YY)
- ['a', '1', '1', '0', '1', '2', '2', '0', '1']
- ['a', '1', '1', 'b', '1', '2', '2', '0', '1']
- ['a', '1', '1', 'b', '1', '2', '2', 'c', '1']
- ['a', 'd', '1', 'b', '1', '2', '2', 'c', '1']
- ['a', 'd', 'e', 'b', '1', '2', '2', 'c', '1']
- ['a', 'd', 'e', 'b', 'f', '2', '2', 'c', '1']
- ['a', 'd', 'e', 'b', 'f', '2', '2', 'c', 'g']
- ['a', 'd', 'e', 'b', 'f', 'h', '2', 'c', 'g']
- ['a', 'd', 'e', 'b', 'f', 'h', 'i', 'c', 'g']
- ['a', 'd', 'e', 'b', 'f', 'h', 'i', 'c', 'g']
- ['0', '1', '1', '0', '1', '2', '2', '0', '1']
Add Comment
Please, Sign In to add comment