Hoffmann
Hoffmann

Reputation: 1082

stacked generator

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

Answers (2)

NPE
NPE

Reputation: 500167

(word for line in input_file for word in line.split())

Upvotes: 1

ovgolovin
ovgolovin

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

Related Questions