【问题标题】:Is there a way int is converted to list using python [duplicate]有没有办法使用 python 将 int 转换为列表 [重复]
【发布时间】:2025-12-02 16:10:02
【问题描述】:

我尝试在列表中添加两个数字。生成的结果是整数。我想使用列表的追加函数将此整数添加到现有列表中。但是,我收到错误,操作不可执行。

Fibo 是一个已定义的列表

Fibo_FV = Fibo[i] + Fibo[i+1] 

print(Fibo_FV)
##result is sum of two numbers in the list

Fibo_final = Fibo.append(Fibo_FV)

print(Fibo_final)
##Answer is none

我不确定为什么在打印 Fibo_final 时没有看到任何内容。我的期望是它应该是带有 Fibo 和新附加值的新列表。关于这个有什么想法吗?

【问题讨论】:

  • 我不确定为什么在打印 Fibo_final 时没有看到任何内容 .append() 方法不返回任何内容,因此默认情况下您会得到 NoneFibo.append(Fibo_FV) 就地修改列表。

标签: python list fibonacci


【解决方案1】:

如下分别做这两个

Fibo.append(Fibo_FV)
Fibo_final = Fibo
print(Fibo_final)

为什么会发生这种情况是因为 append 是一个函数并且它不返回任何内容,所以你的 Fibo_final 包含 None。

【讨论】:

    【解决方案2】:
    Fibo_final = Fibo.append(Fibo_FV)
    

    函数list.append 修改列表并返回None。 许多其他内置操作也是如此,list.extenddict.updateset.add 等。

    你可以这样做

    Fibo.append(Fibo_FV)
    print(Fibo)
    

    改为

    【讨论】:

      最近更新 更多