【问题标题】:Python sorting list with negative number带有负数的Python排序列表
【发布时间】:2017-02-24 11:52:01
【问题描述】:

为了通过实践学习python,我正在尝试使用python实现和测试快速排序算法。

实现本身并不难,但是排序的结果有点令人费解:

当我对列表进行排序时

['35', '-1', '-2', '-7', '-8', '-3', '-4', '20', '-6', '53' ]

结果给了我

['-1', '-2', '-3', '-4', '-6', '-7', '-8', '20', '35', '53' ]

所以列表是排序的,但是负整数是按相反的顺序排序的。

我怀疑这可能是因为我正在对从文件中读取的 int 列表进行排序,而 int 的类型实际上不是 int 而是其他东西(也许是字符串。)我能做什么怎么解决这个问题?

这里是快速排序实现的代码

#quicksort -> the conquer part of the algorithm
def quicksort(input_list, start_index, end_index):
    if start_index < end_index:
        #partition the list of integers.
        pivot = partition(input_list, start_index, end_index)
        #recursive call on both sides of the pivot, that is excluding the pivot itself
        quicksort(input_list, start_index, pivot-1)
        quicksort(input_list, pivot+1, end_index)
    return input_list

#divide part of the algorithm
def partition(input_list, start_index, end_index):
    #declare variables required for sorting
    pivot = input_list[start_index]
    left = start_index + 1
    right = end_index
    sorted = False

    while not sorted:
        #break condition so that left index is crossed with right index
        #or if the value of left crosses the pivot value
        while left <= right and input_list[left] <= pivot:
            #increment left index
            left = left + 1
        #break the loop when right and left indexes cross
        #or if the right value crosses the pivot value
        while right >= left and input_list[right] >= pivot:
            right = right-1
        if right < left:
            sorted = True
        else:
            #swap places for left value and the right value cause they are not in order
            temp = input_list[left]
            input_list[left] = input_list[right]
            input_list[right] = temp
    #swap the value at start index with what's now at the right half. Then return right for the new pivot
    temp = input_list[start_index]
    input_list[start_index] = input_list[right]
    input_list[right] = temp
    return right

感谢任何帮助。感谢大家的时间和帮助。

【问题讨论】:

    标签: python python-2.7 sorting quicksort negative-number


    【解决方案1】:

    您的代码行为正确,因为字符串按字典顺序排序(按第一个字符,如果第一个匹配则为第二个,如果第二个匹配则为第三个,等等)。如果你想按数字排序,最简单的方法是修复你的list,所以它实际上是int 值:

    # Possibly read from file as list of string
    strlist = ['35', '-1', '-2', '-7', '-8', '-3', '-4', '20', '-6', '53']
    
    intlist = map(int, strlist)  # list(map(int, strlist)) on Python 3
    
    quicksort(intlist)
    

    如果需要,您可以在之后转换回 str。否则,您的替代方法是实现对 key 函数的支持(允许您对计算值进行排序),如 list.sort/sorted,但这可能更复杂,您现在想要处理。

    【讨论】:

    • 谢谢!我将 int 映射到列表,一切正常。所以这不是代码中的错误,而是我正在排序的类型及其行为中的错误。谢谢影子游侠。我学到了很多!
    • 感谢您同时包含 python2python3 实现。为此+1。我认为这比对要比较的每个整数进行类型转换更容易、更干净。
    【解决方案2】:

    您正在排序字符串而不是数字,因此它们按字母顺序而不是数字顺序排序。 int() 函数可以将字符串转为数字。

    【讨论】:

      【解决方案3】:

      你的数字都是字符串。如果您只希望输入中包含正整数或负整数,请在进行比较时将它们用int() 包装起来。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-08
        • 1970-01-01
        • 2018-10-23
        • 2010-11-09
        相关资源
        最近更新 更多