<p>您可以对两个列表进行间接排序,然后使用索引将一个列表无序排列为另一个列表的顺序:</p>
<pre><code>>>> from operator import itemgetter
>>>
# extract relevant columns
>>> g1 = list(map(itemgetter(1), ids_and_genres))
>>> g2 = list(map(str.strip, map(itemgetter(0), map(itemgetter(0), genres_and_other_values))))
>>>
>>> assert len(g1) == len(g2)
>>>
# indirectly sort
>>> o1 = sorted(range(len(g1)), key=g1.__getitem__)
>>> o2 = sorted(range(len(g2)), key=g2.__getitem__)
>>>
# allocate and then fill the result
>>> result = len(g2) * [None]
# the loop traverses both result and ids_and_genres in alphabetical
# o1 tells it where to find each element in ids_and_genres and o2
# tells it where that element should go
# this assumes that g1 and g2 have exactly the same elements
# the assertion double checks this
>>> for i1, i2 in zip(o1, o2):
... assert g1[i1] == g2[i2]
... result[i2] = ids_and_genres[i1][::-1]
...
>>>
>>> from pprint import pprint
>>> pprint(result)
[('mpb', 'spotify:track:10KJMfYgg6CORIhtkPn04i'),
('samba', 'spotify:track:59UvKSCJLFDsGOSESlzfAg'),
('forro', 'spotify:track:0ZoJ7jZBHY6SjhbClU7p2B'),
('sertanejo tradicional', 'spotify:track:49usrgIx7kiHYIFWxzYgIC'),
('pagode', 'spotify:track:34646YmFkOhjWcBLhLhIoC'),
('bossa nova', 'spotify:track:7ac8sivblLQjsQipO5FZ63'),
('sertanejo', 'spotify:track:6lGieRasxAycezIC93ofXM'),
('baile pop', 'spotify:track:2C5WKzZ28VrAnMRo7CaTDe'),
('sertanejo universitario', 'spotify:track:3QhJAGDdxt749dFveYVy4M'),
('rock gaucho', 'spotify:track:6jPkYQ3B7b753JTZhR207H'),
('brazilian rock', 'spotify:track:0XcuBfglpHdhOoIdaSEjAv'),
('brazilian punk', 'spotify:track:4UhSLr0vsqzV88uixMt7ca'),
('axe', 'spotify:track:6xR9X79wQ2RGQCK0oBeSnt'),
('brazilian hip hop', 'spotify:track:2D6xFqZdBfdXndPQV4GBLm'),
('sertanejo pop', 'spotify:track:4r8uJGqC1tB16592dPrJA8'),
('hip hop tuga', 'spotify:track:0gFm2Uv3wveMbcqbdNvr2N'),
('hip hop', 'spotify:track:6JRUorZc2SfckySosQKSkz'),
('rap', 'spotify:track:5ggZKufUobrPQATp4URED4'),
('deep funk carioca', 'spotify:track:6A07yoIkMuRuMVHMlpKx5e'),
('grime', 'spotify:track:0cbbgNAPNI8ylaFE0te1yw'),
('brazilian reggae', 'spotify:track:4JWvILyOVmhPqXgSSjVB4p'),
('baile funk', 'spotify:track:6Du0r4fahau45X9bkXIIjB'),
('brazilian electronica', 'spotify:track:3LO1I8lQSsXD6gVdU9KZA1')]
</code></pre>