Python合并排序
我需要编写一个Python合并排序,python,sorting,merge,Python,Sorting,Merge,我需要编写一个merge\u sort和一个相应的merge\u two\u half函数,对元组的参数列表进行排序。列表应按相反顺序排序(最大-最小)。元组列表中作为参数传递给merge\u sort函数的每个元组包含两个元素,merge\u sort函数应使用每个元组的第一个元素(字符串)作为排序键 以便: def main(): print("1.") a_list = [0, 0, 0, 0, 0] list_of_tuples_left = [('Tim
merge\u sort
和一个相应的merge\u two\u half
函数,对元组的参数列表进行排序。列表应按相反顺序排序(最大-最小)。元组列表中作为参数传递给merge\u sort
函数的每个元组包含两个元素,merge\u sort
函数应使用每个元组的第一个元素(字符串)作为排序键
以便:
def main():
print("1.")
a_list = [0, 0, 0, 0, 0]
list_of_tuples_left = [('Tim', 194493), ('Song', 201670), ('Abe', 203126)]
list_of_tuples_right = [('Jim', 194169), ('Ben', 179619)]
merge_two_halves(a_list, list_of_tuples_left, list_of_tuples_right)
print(a_list)
print()
print("2.")
a_list = [0, 0, 0, 0, 0, 0, 0, 0]
list_of_tuples_left = [('Joseph', 194493), ('Ethan', 201670), ('Christopher',
203126),('Andrew', 202301)]
list_of_tuples_right = [('William', 194169), ('David', 179619), ('Anthony',
191681), ('Alexander', 178646)]
merge_two_halves(a_list, list_of_tuples_left, list_of_tuples_right)
print(a_list)
print()
print("3.")
names_id_tuples = get_list_of_tuples_from_file("namesId.txt")
merge_sort(names_id_tuples)
for i in range(30):
print(names_id_tuples[i])
print()
我甚至不知道从哪里开始。幻灯片上的任何帮助都会非常棒。查找合并排序,然后复制并粘贴它,它应该包含合并和合并两部分。一旦将其粘贴到def merge(?):和def merge_两半(?)下方:
那么你唯一需要改变的就是在合并的两半中,你可以试着找出哪一个。你应该了解
收益率
,这使得合并非常简单。tryheapq.merge()
。如有必要,使用自定义的key()
函数包装项目。在这种情况下,您可以考虑重新编辑合并排序算法的现有代码。