Frog_Aurora
Frog_Aurora

Reputation: 83

Python generator function with predicate

def group_func(iterable,p):

    for i in iterable:
         yield [i]
         if p(i):
             yield i

I'm working on group_func

However, the group_func I defined above could not do this...obviously. Any help?

Upvotes: 0

Views: 473

Answers (1)

co2y
co2y

Reputation: 179

you want to split words like this?

def hide(iterable):
    for v in iterable:
        yield v

def group_func(iterable,p):
    result = []
    for i in iterable:
         result.append(i)
         if p(i):
             yield result
             result = []
    yield result


print([v for v in group_func('combustibles', lambda x : x in 'aeiou')])
print([v for v in group_func(hide('combustibles'), lambda x : x in 'aeiou')])

Upvotes: 4

Related Questions