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,

CodePudding user response:

refer to the second floor hbu_pig 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]


This show is day, completely look not to understand ha ha

CodePudding user response:

The
reference 3 floor fortheother response:
1, you can use del not 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 to delete data,


Boon boon, remove and del usage I know, I just don't know what they call syntax...

Del dinner shows, dinner has not been defined, is to put as variables rather than list?



Thank the bosses spite of being very busy toglance reply to my question,

CodePudding user response:

My point is:
Using the list. The copy () method
Make sure
The whole for to recycle

CodePudding user response:

 # test for loop directly deleted sequences inside 
F=[' a ', 'd', 'e']
Tip #=1
For x in f:
Del f [0]
Print (STR (tip) + "delete")
Print (x)
Print (f)
Tip #=tip # + 1

Print (tip)
Print (f)
"" "
Output:
Once deleted
A
[' d ', 'e']
2 times to delete
E
[' e ']
3
[' e ']

This is I just in my computer for some of the operation, said's point of view:

I think this cycle as such have no specification, because your code is to cycle in the sequence f,
But you to delete, f the sequence
Myself at the time of using a loop, such as for x in f
So, what I did behind the cycle of operations are aimed at the x, rather than to manipulate, the f
For the elements in the f you operation, because f element increase reduced factors, will lead to your recurring difficult to understand,

It is as if you go to a restaurant, but the somebody else smashed the same restaurant, the results may have caused you can't eat well, eat

As to why you would be an error, but also for you in the loop using the del,
For x in f:
Del f
After when the loop for the first time, f is the whole deleted, so when the loop to a second later, just can't find the f,
If you want to delete the entire f operation, do not need to write cycle, write directly del f,


PS:
I am just learning this new, come on together.

"" "

CodePudding user response:

refer to 6th floor hbu_pig response:
my point is:
Using the list. The copy () method
Make sure
The whole for recycle
,

See ~ get to a new way of thinking,

CodePudding user response:

refer to 7th floor fortheother response:
 # test for loop directly deleted sequences inside 
F=[' a ', 'd', 'e']
Tip #=1
For x in f:
Del f [0]
Print (STR (tip) + "delete")
Print (x)
Print (f)
Tip #=tip # + 1

Print (tip)
Print (f)
"" "
Output:
Once deleted
A
[' d ', 'e']
2 times to delete
E
[' e ']
3
[' e ']

This is I just in my computer for some of the operation, said's point of view:

I think this cycle as such have no specification, because your code is to cycle in the sequence f,
But you to delete, f the sequence
Myself at the time of using a loop, such as for x in f
So, what I did behind the cycle of operations are aimed at the x, rather than to manipulate, the f
For the elements in the f you operation, because f element increase reduced factors, will lead to your recurring difficult to understand,

It is as if you go to a restaurant, but the somebody else smashed the same restaurant, the results may have caused you can't eat well, eat

As to why you would be an error, but also for you in the loop using the del,
For x in f:
Del f
After when the loop for the first time, f is the whole deleted, so when the loop to a second later, just can't find the f,
If you want to delete the entire f operation, do not need to write cycle, write directly del f,


PS:
I am just learning this new, come on together.

"" "


Very attentively, my understanding is the same with you

CodePudding user response:

You should see more knowledge of Python, don't ask me anything, this is not for learning

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related