我需要编写一个mergeSort函数来合并两个文件,并根据列表中的一个单词按字母顺序对文件中包含的列表进行排序。 合并后,文件将如下所示:
['Bud', 'Abbott', 51, 92.3]
['Mary', 'Boyd', 52, 91.4]
['Jill', 'Carney', 53, 76.3]
['Jeff', 'Zygna', 50, 82.1]
['Don', 'Adams', 51, 90.4]
['Randy', 'Newman', 50, 41.2]
['Fred', 'Quicksand', 51, 88.8]
['John', 'Ziley', 53, 90.1]
名单按顺序排列:名字,姓氏,课程,年级。我要做的是在合并后根据姓氏按字母顺序对列表进行排序。我该怎么开始呢?在
告诉
sort
函数应该使用哪个列表项(key
)进行排序。在请注意,
sort()
对列表进行了适当的排序,而sorted()
则创建了一个新列表。有关详细信息,请参阅文档:Sorting HOW TO。在其他答案已经指出了如何根据每个元素列表中的特定索引对列表进行排序。但是,如果必须手动合并:
假设您有列表:
您可以使用标准的
sorted
函数按姓氏(即每个列表的第二个元素)对其进行排序,并提供一个key
函数从列表中提取姓氏并将其用作比较项。在以下是您需要的:
^{pr2}$如果要修改现有列表,也可以使用
.sort()
方法:相关问题 更多 >
编程相关推荐