我有一个字符串列表,我想使用itertools.compress.
我有大量的字符串需要对照句子列表进行检查。因此,我想使用 itertools 来节省资源。无法按预期工作的部分是通过压缩进行的布尔屏蔽。
from itertools import product, starmap, compress
def is_in(string, other_string):
return string in other_string
to_find = ['hello', 'bye']
some_sentences = ['hello to you', ' hello and bye', 'bye bye']
cartesian = product(to_find, some_sentences)
matched_mask = starmap(is_in, cartesian)
matched = compress(cartesian, matched_mask)
print(list(matched))
actual_result = [('hello', 'hello to you'), ('bye', ' hello and bye')]
expected = [('hello', 'hello to you'),
('hello', 'hello and bye'),
('bye', ' hello and bye'),
('bye', 'bye bye')]
慕的地6264312
相关分类