带predi的Python生成函数

2024-04-19 21:40:25 发布

您现在位置:Python中文网/ 问答频道 /正文

def group_func(iterable,p):

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

我在做组功能

然而,我上面定义的组函数不能做到这一点……显然。有什么帮助吗?在


Tags: 函数in功能forif定义defgroup
1条回答
网友
1楼 · 发布于 2024-04-19 21:40:25

你想这样分词吗?在

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')])

相关问题 更多 >