因此,如果列表中有多个句子并且我想搜索一个词,我该怎么做才能只搜索每个句子的第一个词?

So if there's multiple sentences in a list and I want to search for a word, how do I make it so it only searches the first word of each sentence?

list = ["This is an example", "Hello there! This is an example"]


search = input("Search: ")

for title in list:
    if search in title:
        print(title)
#Output
Search: This
This is an example
Hello there! This is an example

所以它的作用是搜索附加到 (search) 的字符串,但我只希望它搜索作为句子第一个字母的关键字。因此,由于我在(搜索)中输入了“This”,我希望它只搜索并打印“This is an example”,因为“This”是句子的第一个词。

split 句子并与第一个单词进行比较,如下所示:

l = ["This is an example", "Hello there! This is an example"]
search = input("Search: ")
for title in l:
    if search == title.split()[0]:
        print(title)

使用列表理解的较短答案:

l = ["This is an example", "Hello there! This is an example"]
s = input("Enter search string : ").lower()   #lower function for case-less comparison
print("".join([i for i in l if i.split()[0].lower() == s]))

如果您不了解列表理解概念,我不会建议您回答这个问题。可能会通过@not_speshal 寻求答案,因为我的是她代码的较短版本。