Python可以';t串联列表,因为它们包含非类型元素

Python可以';t串联列表,因为它们包含非类型元素,python,list,concatenation,quicksort,nonetype,Python,List,Concatenation,Quicksort,Nonetype,我正在尝试用python实现快速排序。这是我的密码: def quicksort(numbers): less = [] is_pivot = [] larger = [] if len(numbers) > 1: pivot = numbers[0] for x in numbers: if x < pivot: less.append(x)

我正在尝试用python实现快速排序。这是我的密码:

def quicksort(numbers):
    less = []
    is_pivot = []
    larger = []

    if len(numbers) > 1:
        pivot = numbers[0]
        for x in numbers:
            if x < pivot:
                less.append(x)
            elif x == pivot:
                is_pivot.append(x)
            else:
                larger.append(x)
        sorted_list = quicksort(less) + is_pivot + quicksort(larger)
        print(sorted_list)
    else:
        print(numbers)

非类型元素从何而来,如何解决我的问题?谢谢

您的函数返回
,此时您需要返回如下内容:

def quickSort(numbers):
    less = []
    is_pivot = []
    larger = []

    if len(numbers) <= 1:
        return numbers
    else:
        pivot = numbers[0]
        for x in numbers:
            if x < pivot:
                less.append(x)
            elif x > pivot:
                larger.append(x)
            else:
                is_pivot.append(x)
        less = quickSort(less)
        larger = quickSort(larger)
        return less + is_pivot + larger

test = [87, 3, 42, -893, -5, 107, 3, 27, 0]
test = quickSort(test)
print(test)

试试看

好吧,问题完全来自这一行:

sorted_list = quicksort(less) + is_pivot + quicksort(larger)
仔细想想,你的函数不会返回任何东西。因此,
快速排序(较小)
快速排序(较大)
将返回
None

因此,
sorted\u list
变为
[None[2.0],None]

基本上,用
return
语句替换
print
行,然后在函数外部打印

以下是我的解决方案:

def quicksort(numbers):
    less = []
    is_pivot = []
    larger = []

    if len(numbers) > 1:
        pivot = numbers[0]
        for x in numbers:
            if x < pivot:
                less.append(x)
            elif x == pivot:
                is_pivot.append(x)
            else:
                larger.append(x)
        sorted_list = quicksort(less) + is_pivot + quicksort(larger)
        return sorted_list # Replaced print statement here.
    else:
        return numbers  # Replaced print statement here.

print(quicksort([1,3,4,2,5,0]))
def快速排序(数字):
减=[]
is_pivot=[]
较大的=[]
如果len(数字)>1:
枢轴=数字[0]
对于数字中的x:
如果x<枢轴:
减.追加(x)
elif x==枢轴:
is_pivot.append(x)
其他:
较大。追加(x)
排序列表=快速排序(较小)+是透视+快速排序(较大)
在此处返回排序的#列表#替换的打印语句。
其他:
返回编号#替换此处的打印语句。
打印(快速排序([1,3,4,2,5,0]))

您没有
return
语句,因此您的函数返回
None
否决票,因为您没有实际解释错误所在。据我所知,我必须将本应打印排序列表的行改为return语句?因为我试过了,我也犯了同样的错误。所以现在我不再打印(排序列表),而是返回排序列表,请查看我刚才添加的完整解决方案。我用return语句替换了所有print语句(包括
else
语句中的基本情况),它工作正常。啊,问题是我没有在else语句中执行return语句。非常感谢。
[-893, -5, 0, 3, 3, 27, 42, 87, 107]
sorted_list = quicksort(less) + is_pivot + quicksort(larger)
def quicksort(numbers):
    less = []
    is_pivot = []
    larger = []

    if len(numbers) > 1:
        pivot = numbers[0]
        for x in numbers:
            if x < pivot:
                less.append(x)
            elif x == pivot:
                is_pivot.append(x)
            else:
                larger.append(x)
        sorted_list = quicksort(less) + is_pivot + quicksort(larger)
        return sorted_list # Replaced print statement here.
    else:
        return numbers  # Replaced print statement here.

print(quicksort([1,3,4,2,5,0]))