如何使用 Python 中的字符串搜索关键字

How to search for keywords using strings in Python

嗨,所以我需要一些帮助来创建字符串,这些字符串将抓取包含提及 Yeezy + Footpatrol 的推文。

所以只有提到 Footpatrol 时它才会选择 Yeezy。

我不想专门写可变字符串例如:

t = ['Hello world!',
    'Hello World!',
    'Hello World!!!',
    'Hello world!!!',
    'Hello, world!',
    'Hello, World!']

我只想在同一行文本中同时提到 footpatrol 和 Yeezy 时使用嵌套 for 循环推送转发。

使用 in 关键字:

if 'footpatrol' in line and 'Yeezy' in line:
    # Do your stuff
    # Pass

你不提大小写重要吗?这是一个与大小写无关的解决方案。

lines = ["footpatrol", "yeezy footpatrol", "foo bar", "Yeezy and Footpatrol"]
keywords = ["footpatrol", "yeezy"]

for line in lines:
    if all(kw in line.lower() for kw in keywords):
        print(line)

上面会打印

yeezy footpatrol
Yeezy and Footpatrol