简体   繁体   English

python中remove() function是怎么执行的?

[英]How remove() function was executed in python?

Actually I was working with this code:实际上我正在使用这段代码:

m=[0,1,2,3,4,5,6,7]
for i in m:
    m.remove(i)
print(m)

This looks pretty simple.这看起来很简单。

for i in m:
      print(i)

will print every elements in the list m.将打印列表 m 中的每个元素。 So the for part in our code will extract every elements of the list and remove it one by one.因此我们代码中的for部分将提取列表中的每个元素并将其一一删除。 So the output that I except is [].所以我排除的output是[]。

But I got [1,3,5,7].但是我得到了 [1,3,5,7]。

How it can be the output and where I was wrong?怎么可能是 output,我哪里错了?

when you use (for i in m), i starts from index zero so i is the element of index 0 which its value is 0 then in for loop you'll delete zero value.当你使用 (for i in m) 时, i从索引 0 开始,所以i是索引 0 的元素,它的值为 0 然后在 for 循环中你将删除零值。

now your list is [1,2,3,4,5,6,7] and i is the element of index 1 which its value is 2.现在你的列表是 [1,2,3,4,5,6,7] 并且i是索引 1 的元素,它的值为 2。

after deleting 2 your list would be like [2,3,4,5,6,7], now i is the element of index 2 which its value is 4 and....删除 2 后,您的列表将类似于 [2,3,4,5,6,7],现在i是索引 2 的元素,其值为 4 和...

here is how this for loop works and why the output is like that.这是 for 循环的工作原理以及为什么 output 是这样的。

to remove all element in a list you can use:要删除列表中的所有元素,您可以使用:

for i in range(len(m)):
    m.remove(m[0]) 

or或者

m.clear()

What happens here is, you're removing items in the list on the go.这里发生的是,您正在删除 go 列表中的项目。

Eg,例如,

  1. On the first remove() call, it removes 0 from the list (current index is 0).在第一次 remove() 调用时,它从列表中删除 0(当前索引为 0)。

  2. On the second remove() call, the current index is 1, but the list contains [1,2,3,...].在第二次 remove() 调用中,当前索引为 1,但列表包含 [1,2,3,...]。 Now the the index 1 contains "2" instead of "1".现在索引 1 包含“2”而不是“1”。 Because 0 has been removed and the size of list is just changed.因为 0 已被删除并且列表的大小刚刚更改。

So to clear list in python, you can use,所以要清除 python 中的列表,您可以使用,

m.clear()

If you know how to use a debugger, that would be the ideal way to diagnose this, otherwise, try running this code to see what's going on:如果您知道如何使用调试器,那将是诊断此问题的理想方法,否则,请尝试运行此代码以查看发生了什么:

x = [0, 1, 2, 3, 4, 5, 6, 7]
for i in x.copy():
    x.remove(i)
    print(x)
print(x)


m = [0, 1, 2, 3, 4, 5, 6, 7]
for i in m:
    m.remove(i)
    print(m)
print(m)

During a given execution, the for loop accesses an element and then deletes it.在给定的执行过程中,for 循环访问一个元素然后将其删除。 For example, it first deletes the element at index 0, with value 0, leaving you with [1, 2, 3, 4, 5, 6, 7] .例如,它首先删除索引为 0 且值为 0 的元素,留下[1, 2, 3, 4, 5, 6, 7] Then it moves on to the element at index 1, value 2, and deletes that, leaving you with [1, 3, 4, 5, 6, 7] .然后它移动到索引为 1、值为 2 的元素,并删除它,留下[1, 3, 4, 5, 6, 7]

In general, avoid messing around with a list in Python. If you have to delete all elements, use m.clear() .通常,避免乱用 Python 中的列表。如果必须删除所有元素,请使用m.clear() If you have to delete some elements or do some other stuff, prefer to use list comprehension to create a new object.如果你必须删除一些元素或做一些其他的事情,更喜欢使用列表理解来创建一个新的 object。

i don`t know why it is not remove each item, but i worked on it and it works like this:我不知道为什么它不删除每个项目,但我对它进行了处理,它是这样工作的:

m=[0,1,2,3,4,5,6,7]
while m != []:
    [m.remove(i) for i in m]
print(m)

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

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