【问题标题】:Python can't concatenate lists because they include nonetype elementsPython 无法连接列表,因为它们包含非类型元素
【发布时间】:2016-12-07 12:16:31
【问题描述】:

我正在尝试在 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)

这给了我以下错误信息:

  File "sortingalgorithms.py", line 101, in <module>
quicksort(numbers)
File "sortingalgorithms.py", line 66, in quicksort
sorted_list = quicksort(less) + is_pivot + quicksort(larger)
File "sortingalgorithms.py", line 66, in quicksort
sorted_list = quicksort(less) + is_pivot + quicksort(larger)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'list'

当我尝试在不连接它们的情况下打印列表时,我会得到以下输出,其中包含数字 3、2、1 的列表

[1.0]
[]
[None, [2.0], None]
[]
[None, [3.0], None]

非类型元素来自哪里,我该如何解决我的问题?谢谢

【问题讨论】:

  • 你没有return 语句,所以你的函数返回None

标签: python list concatenation quicksort nonetype


【解决方案1】:

好的,所以问题完全来自这一行:

sorted_list = quicksort(less) + is_pivot + quicksort(larger)

仔细想想,你的函数没有返回任何东西。因此,quicksort(less)quicksort(larger) 将返回 None

因此,sorted_list 变为 [None, [2.0], None]

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

这是我的解决方案:

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

【讨论】:

  • 据我了解,我必须将应该打印排序列表的行改为返回语句?因为我试过了,我得到了同样的错误。所以现在代替 print(sorted_list) 我正在做 return sorted_list
  • 请查看我刚刚添加的完整解决方案。我用 return 语句替换了所有打印语句(包括在else 语句中找到的基本情况),它工作正常。
  • 啊,问题是我没有在 else 语句中做 return 语句。谢谢!
【解决方案2】:

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

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)

输出:

[-893, -5, 0, 3, 3, 27, 42, 87, 107]

试试here!

【讨论】:

  • 投了反对票,因为您实际上没有解释出了什么问题。
猜你喜欢
  • 2022-07-30
  • 2021-11-10
  • 2018-05-27
  • 2013-07-03
  • 2015-09-22
  • 2020-07-07
  • 2019-06-03
  • 1970-01-01
  • 2014-01-19
相关资源
最近更新 更多