擅长:python、mysql、java
<p>“列表通常应该是同质的。对相关数据的异构集合使用元组和字典。”<a href="https://stackoverflow.com/questions/59624083/how-to-sort-list-of-lists-with-alternate-alphabetical-value-maintaining-the-asso#comment105410681_59624083">[1]</a></p>
<p>一种方法是将它们排列为元组,然后对它们进行排序也会更容易</p>
<pre><code>mylist = [['CCND1', '67', 'FAS', '99', 'IRAK3', '92', 'ALG14', '86', 'ADRBK1', '10'], ['PTRX', '95', 'CCNA', '33']]
list_of_tuples = []
for l in mylist:
if len(l) % 2 is not 0:
raise ValueError()
list_of_tuples.append([(l[i], l[i+1]) for i in range(0, len(l), 2)])
for l in list_of_tuples:
l.sort(key=lambda tup: tup[0])
print(list_of_tuples)
# [[('ADRBK1', '10'), ('ALG14', '86'), ('CCND1', '67'), ('FAS', '99'), ('IRAK3', '92')], [('CCNA', '33'), ('PTRX', '95')]]
</code></pre>