Wells
Wells

Reputation: 10969

python generator of dynamic length

I have this:

def get_set(line, n=3):
    words = line.split()
    for i in range(len(words) - n):
        yield (words[i], words[i+1], words[i+2])

for i in get_set('This is a test'):
    print(i)

But as you can see in the yield call, it's hard-coded to work with 3. How can I rewrite the yield line to work with whatever number is passed via the n kwarg?

(the code generators sets of each three consecutive words in a sentence, want it to generate whatever I pass as n)

Upvotes: 0

Views: 99

Answers (3)

River
River

Reputation: 9093

You could always just make a tuple out of the range

def get_set(line, n=3):
    words = line.split()
    for i in range(len(words) - (n-1)):
        yield tuple(words[i:i+n])

Note you need to iterate in range len(words) - (n-1) not len(words)-n to get all consecutive pairs.

With

for i in get_set('This is a very long test'):
    print(i)

This gives:

n=3:

('This', 'is', 'a') ('is', 'a', 'very') ('a', 'very', 'long') ('very', 'long', 'test')

n=4:

('This', 'is', 'a', 'very') ('is', 'a', 'very', 'long') ('a', 'very', 'long', 'test')

Upvotes: 1

Chaker
Chaker

Reputation: 1207

You can use slicing on the list:

def get_set(line, n=3):
    words = line.split()
    for i in range(0, len(words), n):
        yield words[i:i+n]

for i in get_set('This is a test'):
    print(i)

['This', 'is', 'a']
['test']

for i in get_set('This is another very boring test', n=2):
    print(i)

['This', 'is']
['another', 'very']
['boring', 'test']

Upvotes: 0

Joran Beasley
Joran Beasley

Reputation: 113988

for row in zip(*[words[x:] for x in range(n)]):
    yield row

should work I think

 for i in range(len(words)-n):
     yield words[i:i+n]

should also work ...

(cast to tuple if needed ...)

Upvotes: 0

Related Questions