Sort list by another list in Python without merging
本问题已经有最佳答案,请猛点这里访问。
注意:我知道这看起来和很多问题类似,但请稍等片刻,然后完成阅读。
有没有一种方法可以将一个列表按另一个排序,这样我们就不必以某种方式合并这些列表了?
1 2 | items = ["a","b","c","d","e"] minutes_ago = [1, 3, 2, 5, 4] |
结果应该是
我在想一些江户记1(2),但却无法想象如何很好地解决它。
出:
1 | items = ["a","c","b","e","d"] |
号
这是我能想到的最简单的方法:
1 2 3 4 5 6 | In [1]: items = ["a","b","c","d","e"] In [2]: minutes_ago = [1, 3, 2, 5, 4] In [3]: [i for m, i in sorted(zip(minutes_ago, items))] Out[3]: ['a', 'c', 'b', 'e', 'd'] |