为什么在Visual Studio中每次运行Python字符串remove()函数时都会运行两次?

huangapple go评论71阅读模式
英文:

Why does python string remove() function run twice every time in Visual Studio?

问题

我正在尝试编写一个程序,根据索引值从列表中移除字符串,但它一直在每次运行 remove() 时都运行两次。

例如,尝试从相同字符串的列表中删除第二个项目:

list = ['banana', 'banana', 'banana']
index = 1
list.remove(list.pop(index))

然后列表只包含 ['banana']

还有:

list = ['apple', 'banana', 'apple', 'apple', 'banana', 'banana']
index = 1
list.remove(list.pop(index))

返回

['apple', 'apple', 'apple', 'banana']

因此,似乎出现了某种原因导致移除操作运行两次,但 list.pop(index) 仅在第一次运行时才运行。即使将 list.pop(index) 分成单独的变量,问题仍然存在。

这可能是与 Visual Studio Code 有关的错误吗?我不明白为什么 remove() 函数每次都会运行两次。

英文:

I'm trying to make a program remove strings from a list by index value, but it keeps running remove() twice at a time.

For example trying to remove the second item from a list of identical strings:

list = ['banana', 'banana', 'banana']
index = 1
list.remove(list.pop(index))

then list only contains ['banana'].

And:

list = ['apple', 'banana', 'apple', 'apple', 'banana', 'banana']
index = 1
list.remove(list.pop(index))

returns
['apple', 'apple', 'apple', 'banana']

so it seems that for whatever reason the removal is running twice but the list.pop(index) only runs the first time. It still occurs when I break the list.pop(index) into a separate variable

Is this possibly a bug with visual studio code? I don't see why the remove() function would run twice every time.

答案1

得分: 1

你两次移除了该项。

数据结构中,你可以看到pop()函数移除了该项并返回它。然后你将返回的项传递给了remove()函数。所以是的,你移除了它两次。

如果你使用pop()函数,列表将会在原地修改。

列表 = ['banana', 'banana', 'banana']
列表.pop(1)
print(列表)

如果你希望,你可以捕获pop()的返回值。

列表 = ['banana', 'banana', 'banana']
x = 列表.pop(1)
print(列表)

print(x)
英文:

You are removing the item twice.

In DataStructures you can see that pop() removes the item and returns it. Then you are passing that returned item to remove() function. So yes, you are removing it twice.

If you use pop() the list will be modified inplace.

list = ['banana', 'banana', 'banana']
list.pop(1)
print(list)
>>> ['banana', 'banana']

If you want, you can capture the pop value.

list = ['banana', 'banana', 'banana']
x = list.pop(1)
print(list)
>>> ['banana', 'banana']

print(x)
>>> 'banana'

答案2

得分: 0

点在于使用.pop()方法访问所需元素,该方法本身会删除/弹出给定索引处的元素(默认是最后一个索引),然后返回它。

因此,list.remove(list.pop(index)) 会从内到外执行,首先执行 list.pop(index) 部分,正如我们上面提到的,它会删除索引为 index 的元素并返回它,然后将返回的元素值传递给 list.remove(value),按照它的规则,它会去移除列表中此元素的第一次出现,因此你会看到这种似乎是“运行两次”的行为,就是这样。

所以,如果你的目标是删除列表中索引为 index 的元素,你可以通过以下两种方式之一实现:list.remove(list[index])list.pop(index)

例如:

list = ['banana', 'banana', 'banana']
index = 1
list.remove(list[index])
# 列表变为 ['banana', 'banana']

或者

list = ['banana', 'banana', 'banana']
index = 1
list.pop(index)
# 列表变为 ['banana', 'banana']
英文:

The point here is that you access the desired element using .pop() method that itself removes/pops the element at the given index ( default the last index ) and return it.

so this list.remove(list.pop(index)) , executes from in to out first it applies the list.pop(index) part , that as we mention above removes the element at index index and return it , then the returned value of element passed to the list.remove(value) so it by it's rule go and remove the first occurance of this element in list so you face this behaviour that seems like twice run and that's it.

so if your target to remove element at index index of the list , you can do this either by list.remove(list[index]) , or list.pop(index)

list = ['banana', 'banana', 'banana']
index = 1
list.remove(list[index])
# list become ['banana', 'banana']

or

list = ['banana', 'banana', 'banana']
index = 1
list.pop(index)
# list become ['banana', 'banana']

huangapple
  • 本文由 发表于 2023年5月29日 03:34:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定