【问题标题】:Python object (list) seems to lose its type [duplicate]Python对象(列表)似乎失去了它的类型[重复]
【发布时间】:2021-12-27 10:19:29
【问题描述】:

在下面的代码中,它说它不能附加“a”,因为它是 NoneType 类,但就在该行之前,它能够打印列表并打印其类型。为什么下一行会丢失类型?

from itertools import combinations_with_replacement
def output_list_of_feature_names(degree, list_of_feature_names):
    a = ["as", "asd"]
    for i in range(1, degree + 1):
        for item in combinations_with_replacement(list_of_feature_names, r = i):
            print(a)
            print(type(a))
            print(a)
            a = a.append(item)
    
    return a

output_list_of_feature_names(2, ["throttle", "angle", "final_vert_speed"])

【问题讨论】:

标签: python


【解决方案1】:
a = a.append(item)

是违规行。 a.append 是一个就地修改列表并返回 None 的函数。然后将该返回值分配给a

要修复,请更改为:

a.append(item)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 2011-10-03
    相关资源
    最近更新 更多