简体   繁体   English

尝试在使用方法 insert() 时在列表中添加新的最后一个元素

[英]Trying to add a new last element in a list while using the method insert()

thanks for reading.谢谢阅读。

I'm currently learning python, yet a silly question popped in my head.我目前正在学习 python,但我脑子里突然冒出一个愚蠢的问题。

I was writting some lines of code usinng the things I've learnt and I noticed something that doesn't make "sense" (whatever that means) to me.我正在使用我学到的东西编写一些代码行,我注意到一些对我来说没有“意义”(无论这意味着什么)的东西。

Here is my code:这是我的代码:

//printing the last element of my list

numbers = [5, 4 ,3 ,2 ,1]

print(numbers[-1]) 

//changing values in my list

numbers[0] = 1

numbers[-1] = 1



//inserting values on my list

print(numbers)

numbers.insert(0,"Hello")

**numbers.insert(-1, "Hello")**

print(numbers)

the question pops with the line I marked as ****BOLD****: Why I can't modify the last element of my list when I put -1?, I touhght that when you call the element [-1] in a list it shows the last element, why this doesn't work when I'm using the insert () method and calling -1?问题出现在我标记为 ****BOLD**** 的行中:当我输入 -1 时,为什么我无法修改列表的最后一个元素?,我认为当您调用元素时 [-1]在一个列表中它显示了最后一个元素,为什么当我使用 insert () 方法并调用 -1 时这不起作用?

this is what happens when you run all the code.这就是运行所有代码时发生的情况。

1

[1, 4, 3, 2, 1]

['Hello', 1, 4, 3, 2, 'Hello', 1]

When you use insert , you are inserting at the specified index position.当您使用insert ,您将在指定的索引位置插入。 Remember that lst[-x] is the same as lst[len(lst)-1-x] .请记住, lst[-x]lst[len(lst)-1-x] For example, lst[-1] and lst[len(lst)-1-1] both refer to the last index position in the list.例如, lst[-1]lst[len(lst)-1-1]都指的是列表中的最后一个索引位置。 Once you know this, the behavior you are seeing with the negative index positions starts to make more sense:一旦你知道这一点,你看到的负指数位置的行为开始变得更有意义:

nums = [1, 2, 3, 4, 5]

nums.insert(1, 'Hello')  # Insert at index position 1.
# Result: [1, 'Hello', 2, 3, 4, 5]

nums.insert(-1, 'Bye')  # Insert at index position len(nums)-1-1 = 4.
# Same as: nums.insert(4, 'Bye')
# Result: [1, 'Hello', 2, 3, 4, 'Bye', 5]

nums.insert(-2, 'Good')  # Insert at index position len(nums)-1-2 = 5.
# Same as: nums.insert(5, 'Good')
# Result: [1, 'Hello', 2, 3, 4, 'Good', 'Bye', 5]

To insert at the end of the list using insert :使用insert在列表末尾insert

nums = [1, 2, 3]
nums.insert(len(nums), 'Hello')
# Result: [1, 2, 3, 'Hello']

As you can see, 'Hello' was inserted in index position 3 (the original len(nums) ) as specified.如您所见, 'Hello'已按照指定插入索引位置3 (原始len(nums) )。

If all you want to do is add an element at the end of a list, you could use append instead:如果您只想在列表末尾添加一个元素,则可以使用append代替:

nums = [1, 2, 3]
nums.append('Hello')
# Result: [1, 2, 3, 'Hello']

To insert at the end of a list, you have to use要在列表末尾插入,您必须使用

numbers.insert(len(numbers), 'Hello')

This will output这将输出

[5, 4, 3, 2, 1, 'Hello']

The problem you faced occurs because numbers[-i] is equivalent to numbers[len(numbers) - 1 - i] .您遇到的问题是因为numbers[-i]等效于numbers[len(numbers) - 1 - i] So, numbers[-1] is actually numbers[5 - 1 - 1] , ie, numbers[3] , which is exactly what was happening in your program.因此, numbers[-1]实际上是numbers[5 - 1 - 1] ,即numbers[3] ,这正是您的程序中发生的事情。 Insertion was happening at the 3rd index.插入发生在第三个索引处。

Why I can't modify the last element of my list when I put -1?, I touhght that when you call the element [-1] in a list it shows the last element, why this doesn't work when I'm using the insert () method and calling -1?为什么我在输入 -1 时无法修改列表的最后一个元素?,我认为当您在列表中调用元素 [-1] 时,它会显示最后一个元素,为什么这在我输入时不起作用使用insert()方法并调用-1?

Because -1 is len(x)-1 , it's the position of the last item but it "points to" right before the last item 0 , in the same way 0 points right before the first item.因为-1 len(x)-1 ,这是最后一个项目的位置,但它“点”右边的最后一个项目之前0,以同样的方式为0分的第一个项目之前。 Basically:基本上:

 [a][b][c][d][e]
 0  1  2  3  4  5
-5 -4 -3 -2 -1

Thus in the same way insert(0, ...) inserts before the first item, insert(-1, ...) inserts right before the last item.因此,以同样的方式insert(0, ...)在第一项之前insert(-1, ...)insert(-1, ...)在最后一项之前插入。

To add an element at the end, you need to insert right after the last item.要在最后添加元素,您需要在最后一项之后插入。

[0] that's the "pointer" view of indexing, shared by most 0-indexed languages: the index is seen as an offset from the array pointer. [0] 这是索引的“指针”视图,由大多数 0 索引语言共享:索引被视为数组指针的偏移量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM