简体   繁体   中英

Sort list based on how another list is sorted

I have 2 list like this:

    A = [10, 29, 30, 49, 21, 3, 5, 6]
    B = [y, z, t, e, q, f, h, d]

I want list B to sort the same way as list A after sort() function, example of output:

    A.sort()
    print(A) 
    >>>[3, 5, 6, 10, 21, 29, 30, 49]
    print(B)
    >>>[f, h, d, y, q, z, t, e]

Zip the two lists together. Sort on the first item of each pair, and then unzip.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM