Python Merge Sort

时间:2014-10-15 20:02:14

标签: python sorting merge

我需要编写一个merge_sort和一个相应的merge_two_halves函数来对元组的参数列表进行排序。列表应按相反顺序排序(最大 - 最小)。元组列表中作为参数传递给merge_sort函数的每个元组都包含两个元素,merge_sort函数应该使用每个元组的第一个元素(一个字符串)作为排序键。 / p>

这样:

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()

我甚至不知道从哪里开始。任何帮助都很棒

1 个答案:

答案 0 :(得分:0)

在幻灯片上查找合并排序,只需复制并粘贴它,它应该用于merge和merge_two_halves。一旦你粘贴在def合并下面(?):和def merge_two_halves(?): 那么你唯一需要改变的是一个<到>在merge_two_halves中,您可以尝试找出哪一个。