【问题标题】:need to add using .append() to a copy of a list [closed]需要使用 .append() 添加到列表的副本[关闭]
【发布时间】:2022-11-04 05:47:40
【问题描述】:

我必须使用 .copy 创建一个名为“list_extra”的“list”副本。新列表是 'list_extra' 所以 list_extra = list.copy 是我的第一行代码。 然后我需要使用 append 添加到这个。我尝试过的任何代码行都会返回错误。

如果您知道如何将附加与列表副本一起使用,请回答。

到目前为止,我已经尝试过:

storm_list_extra = storm_list.copy(append('Quentin'))
 

storm_list_extra = storm_list.copy
storm_list.copy.append('Quentin')


storm_list_extra = storm_list.copy
storm_list_extra.append('Quentin')
storm_list_extra

都返回错误:

'builtin_function_or_method' object has no attribute 'append'

【问题讨论】:

  • 第一行应该是storm_list_extra = storm_list.copy()。然后您可以继续您的append() 操作。

标签: python list copy append


【解决方案1】:
storm_list_extra = storm_list.copy

与:

storm_list_extra = storm_list.copy()

在您的情况下,您将函数stormlist.copy 分配给stormlist_extra,然后尝试在该函数上调用append

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 2019-12-11
    • 2021-06-22
    相关资源
    最近更新 更多