【问题标题】:index function not assigned to duck未分配给鸭子的索引功能
【发布时间】:2016-11-07 04:17:06
【问题描述】:

我被困在这里

说明

使用.index(item) 函数查找“duck”的索引。将该结果分配给一个名为duck_index 的变量。

然后.insert(index, item)该索引处的字符串“cobra”。

代码

animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
duck_index = animals.index("duck")  

animals.insert(2, "cobra") 

print animals 

【问题讨论】:

  • 给出明确的描述,为什么当你animals.insert时,你不使用duck_index?另外:你的问题是什么?
  • 说真的,说明书给你答案。

标签: python


【解决方案1】:

答案就是问题……

animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
duck_index = animals.index("duck")  # retrieve the index of "duck" and assign it to variable duck_index
animals.insert(duck_index, "cobra") # insert "cobra" at duck_index
print(animals)

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    相关资源
    最近更新 更多