【问题标题】:Function "pop" is returning an error. I am trying to remove an element within the list [duplicate]函数“pop”返回错误。我正在尝试删除列表中的一个元素 [重复]
【发布时间】:2021-08-17 15:55:54
【问题描述】:

我正在尝试使用 pop 函数删除列表中的第 4 个元素。但是,我不断收到一条错误消息。请帮忙。

inventory = ["twin bed", "twin bed", "headboard", "queen bed", "king bed", "dresser",
             "dresser", "table", "table", "nightstand", "nightstand", "king bed",
             "king bed", "twin bed", "twin bed", "sheets", "sheets", "pillow",
             "pillow"]

removed_item = inventory.pop[4]
print(removed_item)

这是不断返回的错误信息;

Traceback (most recent call last):
  File "script.py", line 18, in <module>
    removed_item = inventory.pop[4]
TypeError: 'builtin_function_or_method' object is not subscriptable

【问题讨论】:

  • 您的意思是:'inventory.pop(4)'?
  • 函数始终要求传递给它们的任何参数都放在括号中。

标签: python function


【解决方案1】:

是的,你是对的 pop() 确实有一个索引,但你不会像 pop[4] 这样写它,因此就像 quamrana 说的那样,你应该把 inventory.pop(4) 而不是 inventory.pop[4]

【讨论】:

    猜你喜欢
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    相关资源
    最近更新 更多