Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用非库排序函数(Python)对元组列表进行排序_Python_Sorting - Fatal编程技术网

使用非库排序函数(Python)对元组列表进行排序

使用非库排序函数(Python)对元组列表进行排序,python,sorting,Python,Sorting,正如上面提到的问题,我需要使用一个非库排序函数,特别是我在上一个赋值中实现的mergesort函数。我使用activity\u arr.sort(key=operator.itemgetter(2))完成了我的作业。但是,要求我使用此功能: def merge_sort(array): ''' Sorts an array using merge sort algorithm.''' if len(array) > 1: # '//' is for "floor division"

正如上面提到的问题,我需要使用一个非库排序函数,特别是我在上一个赋值中实现的mergesort函数。我使用
activity\u arr.sort(key=operator.itemgetter(2))
完成了我的作业。但是,要求我使用此功能:

def merge_sort(array):
''' Sorts an array using merge sort algorithm.'''
if len(array) > 1:
    # '//' is for "floor division", used in case array is filled with floats
    mid = len(array) // 2
    left = array[:mid]
    right = array[mid:]

    # Recursion to sort left and right half of array
    merge_sort(left)
    merge_sort(right)

    i = 0
    j = 0
    k = 0

    while i < len(left) and j < len(right):
        if left[i] < right[j]:
            array[k] = left[i]
            i += 1
        else:
            array[k] = right[j]
            j += 1
        k += 1
    # Fill the rest of the array with remaining numbers in each array
    while i < len(left):
        array[k] = left[i]
        i += 1
        k += 1
    while j < len(right):
        array[k] = right[j]
        j += 1
        k += 1
def merge_排序(数组):
''使用合并排序算法对数组排序'''
如果len(阵列)>1:
#“//”表示“地板分割”,用于数组中填充浮点数的情况
mid=len(数组)//2
左=数组[:中]
右=数组[mid:]
#对数组的左半部分和右半部分进行排序的递归
合并排序(左)
合并排序(右)
i=0
j=0
k=0
而i
我假设,我将不得不修改它,以便我可以对元组列表进行排序。我要排序的列表的一个例子是
example_list=[(1,3,4)、(7,2,5)、(1,2,1)]
。我想按元组的第三个元素按升序排序。因此,它应该产生以下结果:
sorted_list=[(1,2,1)、(1,3,4)、(7,2,5)]


我仍然只是在学习python,在CS中还有很多东西需要学习。我不知道如何做出这种改变,尽管进行了大量研究,但我发现大多数人都在说要使用.sort()。如果我遗漏了什么重要的东西,请告诉我!非常感谢任何帮助或信息供我查阅。谢谢大家!

您可以传递一个索引,根据该索引对元组数组进行排序。 例如,如果要根据索引2对数组进行排序(索引从0开始),可以将其用作:
merge\u sort(example\u list,2)
。想法很简单,您必须根据给定的索引比较元素,就像我在这里做的那样:
如果左[i][index]

def merge_sort(array,index):         #edit here
''' Sorts an array using merge sort algorithm.'''
if len(array) > 1:
    # '//' is for "floor division", used in case array is filled with floats
    mid = len(array) // 2
    left = array[:mid]
    right = array[mid:]

    # Recursion to sort left and right half of array
    merge_sort(left,index)        #edit here
    merge_sort(right,index)       #edit here

    i = 0
    j = 0
    k = 0

    while i < len(left) and j < len(right):
        if left[i][index] < right[j][index]:      #edit here
            array[k] = left[i]
            i += 1
        else:
            array[k] = right[j]
            j += 1
        k += 1
    # Fill the rest of the array with remaining numbers in each array
    while i < len(left):
        array[k] = left[i]
        i += 1
        k += 1
    while j < len(right):
        array[k] = right[j]
        j += 1
        k += 1
def merge_sort(数组、索引):#在此处编辑
''使用合并排序算法对数组排序'''
如果len(阵列)>1:
#“//”表示“地板分割”,用于数组中填充浮点数的情况
mid=len(数组)//2
左=数组[:中]
右=数组[mid:]
#对数组的左半部分和右半部分进行排序的递归
合并\排序(左,索引)\在此处编辑
合并\排序(右,索引)\在此处编辑
i=0
j=0
k=0
而i
哇,我觉得自己很傻。这是一个如此简单的解决方案。非常感谢你!如果这是你想要的,你可以接受它作为一个答案。