Possible Duplicate:
Removing from a list while iterating over it
我有这一法典:
s = [2,3,4,5]
for i in s:
s.remove(i)
print(s)
在我执政时,结果是:
[3,5]
这里的逻辑错误是什么?
Possible Duplicate:
Removing from a list while iterating over it
我有这一法典:
s = [2,3,4,5]
for i in s:
s.remove(i)
print(s)
在我执政时,结果是:
[3,5]
这里的逻辑错误是什么?
在修改清单时,你正在对清单进行反复讨论,这正造成你的问题。
列出一份临时名单(list(s)
),供您根据您的需要公布和修改原件:
>>> s = [2,3,4,5]
>>>
>>> for i in list(s):
... s.remove(i)
...
>>> print(s)
[]
合乎逻辑的错误是在通过同一名单删除清单中的内容。
Python hides some implementation details, of course. Internally, you remove the first item, leaving [3,4,5]
. Then you remove the second item, leaving [3,5]
. At this point you have removed two items from a list that is only two items long, so you are done.
这是因为主持人。 3. 审议本守则部分:
s = [2,3,4,5]
for i in s:
print "i",i
j = s
print "j", j
s.remove(i)
print "removed"
print "j", j
print "s", s
print s
产出将是
i 2
j [2, 3, 4, 5]
removed
j [3, 4, 5]
s [3, 4, 5]
i 4
j [3, 4, 5]
removed
j [3, 5]
s [3, 5]
[3, 5]
基本上,当你执行居留权时,内部继承人就跑到[0],然后是第3.1条等。 当下一轮 lo行时,有1 3.1起已经是4起,而不是3起,因为其中本身已经做了修改。
Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...
I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...
Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...
Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...
I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...
Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...
Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...
I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...