取两个由字符串组成的列表:
strings = ['hello everyone!', 'how are you doing?', 'are you doing well?', 'are you okay?', 'good, me too.']
searching_for = ['are', 'you', 'doing']
我的目标是搜索strings中的每个项目searching_for并打印包含这些关键字的完整字符串。即,我希望我的输出是:
Output: ['how are you doing?', 'are you doing well?']
请注意,输出只是 中的第 2 和第 3 项strings,它不包含第 4 项。
我不确定为什么这对我来说如此困难,但我认为这归结为我还不够了解 Python。我想让这个足够通用,这样我就可以在一个非常大的字符串列表中搜索我给它的关键字。到目前为止,这是我的解决方案:
def search(*args):
arg_list = []
search_for = numpy.append(arg_list, args)
for i in strings:
for j in search_for:
if all(j in i) is True:
print(i)
但这会抛出一个TypeError: 'bool' object is not iterable. 我尝试了上述代码的几个不同的迭代,使用 Python 的内置filter函数和其他一些函数,但我一直被类似的错误所困扰。我也不确定这是否会给我一个列表,我认为它会在终端的新行中吐出结果。
慕斯王
幕布斯7119047
人到中年有点甜
幕布斯6054654
慕娘9325324
相关分类