Reputation: 1082
In order to tokenize a file I have a generator that spits out the whitespaceseparated parts of the file, ignoring linebreaks.
def parts(filename):
file = open(filename, 'r')
for line in file:
for part in line.split():
yield part
Now, I'd like to write that as a generator comprehension - what didn't work is:
p = (part for part in line.split() for line in file)
or
p = (part for part in (line.split() for line in file))
The latter spits out the splitted lines instead of the parts of the splitted lines
Upvotes: 1
Views: 75
Reputation: 13410
Use this syntax:
p = (part for line in file for part in line.split())
The outermost for-loop in a generator expression is the left one.
Upvotes: 6