- Using generators can be clearer than the alternative of returning lists of accumulated results
Effective Python
The simplest choice for functions that produce a sequence of results is to return a list of items.
def index_words(text):
result = []
if text:
result.append(0)
for index, letter in enumerate(text):
if letter == ' ':
result.append(index + 1)
return result
address = 'Four score and seven years ago...'
result = index_words(address)
print(result[:3])
[0, 5, 11]
There are two problems with the index_word function.
Each time a new result is found, I call the append method.
There is one line ofr creating the result list and another for returning it.
def index_words_iter(text):
if text:
yield 0
for index, letter in enumerate(text):
if letter == ' ':
yield index + 1
def index_file(handle):
offset = 0
for line in handle:
if line:
yield offset
for letter in line:
offset += 1
if letter == '':
yield offset
from itertools import islice
with open('/tmpaddress.txt') as f:
it = index_file(f)
result = islice(it, 0, 3)
print(list(result))
Recommended Posts