【问题标题】:List: 'NoneType' object is not iterable [duplicate]列表:“NoneType”对象不可迭代[重复]
【发布时间】:2020-02-22 19:12:14
【问题描述】:

代码:

from itertools import permutations
L = list(input().split())
List=[]
for char in L[0]:
    List.append(char)
length = int(L[1])
###
List.sort()  
###
for ele in permutations(List,length):
    for i in ele:
        print(i,end="")
    print()

在编写这段代码时,程序运行得非常好。但 关于将注释行之间的行替换为:

List = List.sort()

此错误正在上升:“NoneType”对象不可迭代 我无法理解这个异常

【问题讨论】:

  • List.sort() 不会创建新列表。它修改了原始列表。

标签: python-3.x list


【解决方案1】:

你不必分配 list.sort() 的结果,列表将被排序,只需使用:

List.sort()

来自docs

此方法在适当的位置修改序列以节省空间 对大序列进行排序。提醒用户它是并行操作的 效果,它不会返回排序后的序列(使用 sorted() 显式请求一个新的排序列表实例)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-08
    • 2020-05-06
    • 1970-01-01
    • 2021-12-05
    • 2013-12-01
    • 2012-08-25
    • 1970-01-01
    相关资源
    最近更新 更多