I have a question about Python Narrator function.

Asked 2 years ago, Updated 2 years ago, 22 views

This is a repeatable object, and I have a question

a = [1,2,3,4]
for i in a: print(a) 

b = iter(a)
for i in b: print(b)

We use the ether function to call up the value one by one, but isn't the conclusion the same? I don't know the value using the ether function except for one time. Is there a way to load the value using next() without a repeat statement?

python

2022-09-22 16:53

1 Answers

If you insist on using iter() in the example you presented, you should do it as follows.

 a = [1,2,3,4]
 b = iter(a)

for i in b:
    print(i)

To answer your question, you can use the following format.

a = [1,2,3,4]
b = iter(a)

while b:
    print b.next()

# The StopIteration excitation processing is omitted.


2022-09-22 16:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.