Home > other > Novice asked a questions about the iterator and a for loop in.
Novice asked a questions about the iterator and a for loop in.
Time:03-13
Recently learning python, question 1: the book youdao problem is to let I delete (list) of the three elements,
At that time, I think this kind of method to delete them one by one, very stupid, think should be able to have a faster, more flexible solution method, the seed down to the question,
When I learn the if found themselves judgement can make the code, just want to can quickly deleted, but later found out that if the else elif is single, no loop iteration, can't do,
To study the for loop in time, I think the puzzle should be solved, but found, the remove () and pop () for the in circulation, is there will be a iterator "dislocation", then turned to try to use del statements, { question 2 : since the book above said del and remove () have what distinction, should be called (statement) and function? }
and then realize that is nobody function or statement of problem, but for in pointer will remove elements because it is over, and auto jump on an element, the loop less than expected, so want to ask, is there any way through my knowledge for continuous removing elements of the same list, until no list?
Suddenly realize error
CodePudding user response:
In this first thank you bosses
CodePudding user response:
& gt;> Ls=[I for I in range (10)] > Ls [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] > For e in ls. Copy () : . Print (e) . If e % 3==0: . Ls. Remove (e) . 0 1 2 3 4 5 6 7 8 9 > Ls [1, 2, 4, 5, 7, 8]
CodePudding user response:
1, can not use del delete all at once? 2, the cycle as you write code, should be able to directly use del dinner, they all deleted, need for circulation processing, 3, del a [1], and delete in the list of the second element, A.r emove (' BBB '), and delete list 'BBB' option, The former by serial number to delete data, the latter through content deleted data,