Iterators
循环的iterator,结构化输出
Iterators vs. iterables
Iterable
Iterator
Iterating over iterables (list)
# Create a list of strings: flash
flash = ['jay garrick', 'barry allen', 'wally west', 'bart allen']
# Print each list item in flash using a for loop
for person in flash:
print (person)
# Create an iterator for flash: superhero
superhero=iter(flash)
# Print each item from the iterator
print(next(superhero))
print(next(superhero))
print(next(superhero))
print(next(superhero))Iterating over dictionaries
Iterating over file connections
Iterating over iterables (range)
Iterators as function arguments
enumerate()
enumerate() and unpack
zip()
zip() and unpack
zip(*object)
Print zip with *
* operator (splat operator)
Use iterator to load data in chunks
Last updated