擅长:python、mysql、java
<p>将两个列表压缩在一起,进行排序,然后选择所需的部分:</p>
<pre><code>>>> yx = zip(Y, X)
>>> yx
[(0, 'a'), (1, 'b'), (1, 'c'), (0, 'd'), (1, 'e'), (2, 'f'), (2, 'g'), (0, 'h'), (1, 'i')]
>>> yx.sort()
>>> yx
[(0, 'a'), (0, 'd'), (0, 'h'), (1, 'b'), (1, 'c'), (1, 'e'), (1, 'i'), (2, 'f'), (2, 'g')]
>>> x_sorted = [x for y, x in yx]
>>> x_sorted
['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']
</code></pre>
<p>将这些组合在一起,得到:</p>
<pre><code>[x for y, x in sorted(zip(Y, X))]
</code></pre>