猿问

数组:以负索引插入

-1 没有在数组的最后一个索引处插入“hello”


如果我有一个数组,x:


>>> x = [1, 2, 3] then

>>> x.insert(-1, "hello")

>>> print(x)

[1, 2, 'hello', 3]

为什么 -1 不在数组的最后一个索引处插入“hello”?由于 -1 索引指的是列表的最后一项,所以我期待:


[1, 2, 3, “你好”]


POPMUISE
浏览 173回答 3
3回答

GCT1015

运行help方法通常会给你这些问题的答案:help(list.insert)#Help on method_descriptor:##insert(self, index, object, /)#    Insert object before index.
随时随地看视频慕课网APP

相关分类

Python
我要回答