简体   繁体   English

根据另一个列表的排序方式对列表进行排序

[英]Sort list based on how another list is sorted

I have 2 list like this: 我有2个像这样的列表:

    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: 我希望列表B在sort()函数之后以与列表A相同的方式排序,输出示例:

    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. 对每对中的第一项进行排序,然后解压缩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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