简体   繁体   中英

python generator for..in vs next()

I'm a bit confused by python's generators, and using a for loop vs using next() on them.

I need to parse a text file that contains information about ie cars on each line, each car having multiple non-mandatory lines after the mandatory line with the model name that starts with an a. . So I want to iterate over the cars in a pythonic fashion.

I have the following bit of code:

lines = """a. car1 model
b. car1 price
c. car1 details
a. car2 model
b. car2 price
a. car3 model
b. car3 price
c. car3 details
d. car3 comments
"""

def iter_cars(lineit):
   res = []
   for line in lineit:
       if line.startswith('a') and res:
           yield res
           res = []

       res.append(line)
   yield res

# print the cars with next..., 
lineit = iter(lines.split('\n'))
print(next(iter_cars(lineit)))
print(next(iter_cars(lineit)))
print(next(iter_cars(lineit)))

# reset the iterator and print the cars with for
lineit = iter(lines.split('\n'))
for car in iter_cars(lineit):
    print(car)

and the output is:

# using next()
a. car1 model
b. car1 price
c. car1 details

b. car2 price

b. car3 price
c. car3 details
d. car3 comments


# using for..in
a. car1 model
b. car1 price
c. car1 details

a. car2 model
b. car2 price

a. car3 model
b. car3 price
c. car3 details
d. car3 comments

Why are the two outputs different? Why is next() skipping the a. line of each car after the first? How can I rewrite the iterator so that using next() yields (chuckles) the same result as using for..in ?

you are instantiating the generator each time. Try this

gen = iter_cars(lineit)
print(next(gen))
print(next(gen))
print(next(gen))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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