在不使用Python内置函数的情况下反转列表顺序

时间:2019-04-01 14:35:21

标签: python python-3.x

我是python的新手,为了变得更好,我购买了一些教科书。我在教科书中发现的一个问题令我感到非常困惑,因为我认为解决方案显而易见。给我一个插入排序代码,该代码按升序排列列表。问题要求在不使用内置函数的情况下反转列表的顺序(因此降序排列),而无需更改代码。这是代码:

sequence = [5,-2,0,6,10,-5,1]


def myInsertionSort(sequence):
    for i in range (1, len(sequence)):
        item = sequence[i]
        j = i
        while j > 0 and sequence[j-1] > item:
            sequence[j] = sequence[j-1]
            j -= 1
            sequence[j] = item
    return sequence

myInsertionSort(sequence)

我添加了一个示例序列,这里的输出是它以升序对序列进行排序。我意识到这里的关键是理解代码,所以我使用pythontutor.com可视化并理解了每个步骤,即使我现在觉得我理解了我对代码进行的每次编辑都会导致错误或无序的列表。

3 个答案:

答案 0 :(得分:0)

考虑一下您的代码的一般化;我刚刚将sequence[j-1]item的比较重构为一个单独的函数。

def out_of_order(a, b):
    return a > b

def myInsertionSort(sequence):
    for i in range (1, len(sequence)):
        item = sequence[i]
        j = i
        while j > 0 and out_of_order(sequence[j-1], item):
            sequence[j] = sequence[j-1]
            j -= 1
            sequence[j] = item
    return sequence

如果while循环将两个项目视为乱序,则会对其进行交换。现在考虑一下,如果通过修改out_of_order的定义来更改两个项目不正常的含义,会发生什么。

答案 1 :(得分:0)

您的书示例不是非常Python化。

这称为插入排序。看看动画如何工作可能会有所帮助。看一下这个网站(确保选择插入[ INS ]排序):https://visualgo.net/bn/sorting

可以说,请密切注意while循环语句>中的sequence[j-1] > item。这是我的提示。

sequence = [5,-2,0,6,10,-5,1]


def myInsertionSort(sequence):
    for i in range (1, len(sequence)):
        item = sequence[i]
        j = i
        while j > 0 and sequence[j-1] < item:
            sequence[j] = sequence[j-1]
            j -= 1
            sequence[j] = item
    return sequence

myInsertionSort(sequence)

如果它更易于阅读,则此处的示例稍有不同:

sequence = [5,-2,0,6,10,-5,1]

def myInsertionSort(sequence):
    for j in range (1, len(sequence)):
        while j > 0 and sequence[j-1] > sequence[j]:
            sequence[j], sequence[j-1] = sequence[j-1], sequence[j] # Flip the elements
            j -= 1 # Move down one index
    return sequence

print(myInsertionSort(sequence))

答案 2 :(得分:0)

一种超级简单的方法就是简单地返回sequence [::-1],它将为您提供一个升序的列表。我不认为列表反向技术是内置功能。<​​/ p>

return sequence[::-1]

但是,如果您认为这很重要,可以使用以下替代代码:

sequence = [5,-2,0,6,10,-5,1]


def myInsertionSort(sequence):
    for i in range (1, len(sequence)):
        item = sequence[i] # for example, when i is 0, item = 5
        j = i # now j = i = 1
        while j > 0 and sequence[j-1] < item:
        # our goal is to push smaller numbers towards the end of the list
        # e.g. when i = 1, j = 1 sequence[j-1] = sequence[0] = 5, so sequence[0] > sequence[1], we want to keep their position, now move on
        #. when i = 1, j = 2, sequence[j-1] = sequence[1] = -2, so sequence[1] < sequence[2], we must switch them
            sequence[j] = sequence[j-1] # sequence[1] is reassigned the current value of sequence[2] because the latter is bigger
            j -= 1 # now we also must put the smaller value in the current sequence[2] position  
            sequence[j] = item # sequence[2] = item (item is the sequence[1]) 
    return sequence

myInsertionSort(sequence)