列表中的python单词

列表中的python单词,python,if-statement,Python,If Statement,我创建了一个在列表中搜索特定单词的小程序。它似乎有效,但我希望它也能打印出它找到的单词 到目前为止,我有这个,但它只返回列表中的第一个单词,即使它不是找到的单词 一些建议,使这项工作将不胜感激。 谢谢 请注意,在words中k的循环中,您只需打印所有单词,而不检查它们是否实际包含在文本中。您还需要拆分文本中的元素以检查结果列表是否包含k。 您希望执行以下操作: for k in words: if k in text.split(): print (k)

我创建了一个在列表中搜索特定单词的小程序。它似乎有效,但我希望它也能打印出它找到的单词

到目前为止,我有这个,但它只返回列表中的第一个单词,即使它不是找到的单词

一些建议,使这项工作将不胜感激。 谢谢


请注意,在words中k的循环
中,您只需打印所有单词,而不检查它们是否实际包含在
文本中。您还需要
拆分
文本中的元素
以检查结果列表是否包含
k
。 您希望执行以下操作:

for k in words:
    if k in text.split():
        print (k)
        print ("word found")
输出


这同样适用于初始条件,如果要初始检查
文本中是否包含任何单词,则需要拆分单词:

any(k in text.split() for k in words)

但是,请注意,正如@Austin所发布的,最适合您尝试执行的工具是
集合
。通过计算交点,可以轻松计算两个集合上的公共元素,如下所示:

set(text.split()) & set(words)
# {'is'}

中查找有关该主题的更多信息请注意,在Word中k的循环
中,您只需打印所有单词,而不检查它们是否实际包含在
文本中。您还需要
拆分
文本中的元素
以检查结果列表是否包含
k
。 您希望执行以下操作:

for k in words:
    if k in text.split():
        print (k)
        print ("word found")
输出


这同样适用于初始条件,如果要初始检查
文本中是否包含任何单词,则需要拆分单词:

any(k in text.split() for k in words)

但是,请注意,正如@Austin所发布的,最适合您尝试执行的工具是
集合
。通过计算交点,可以轻松计算两个集合上的公共元素,如下所示:

set(text.split()) & set(words)
# {'is'}

中查找有关该主题的更多信息您只需执行以下操作(多种操作中的一种):

您只需执行以下操作(多种操作中的一种):


也许会像这样重组

text = 'this is a test'
words =['image', 'is']

words_found = [word for word in words if word in text]
if len(words_found)>0:
    print(words_found)
else:
    print("nope")

    ```

也许会像这样重组

text = 'this is a test'
words =['image', 'is']

words_found = [word for word in words if word in text]
if len(words_found)>0:
    print(words_found)
else:
    print("nope")

    ```

如果要查找公共元素,我建议使用
集合

text = 'this is a test'
words = ['image', 'is']

print(set(words).intersection(text.split()))
# {'is'}

如果要查找公共元素,我建议使用
集合

text = 'this is a test'
words = ['image', 'is']

print(set(words).intersection(text.split()))
# {'is'}
试试这个:

text = 'this is a test'
words =['image', 'is']

for k in [w for w in words if w in text]: 
    print (k) 
    print ("word found")
text = 'this is a test'
words =['image', 'is']

found = False
found_words = []

for k in words:
    if k in text:
        found_words.append(k)
        found = True

if found:
    print("words found:", found_words)
else:
    print("nope")
试试这个:

text = 'this is a test'
words =['image', 'is']

for k in [w for w in words if w in text]: 
    print (k) 
    print ("word found")
text = 'this is a test'
words =['image', 'is']

found = False
found_words = []

for k in words:
    if k in text:
        found_words.append(k)
        found = True

if found:
    print("words found:", found_words)
else:
    print("nope")
试试这个:

text = 'this is a test'
words =['image', 'is']

for k in [w for w in words if w in text]: 
    print (k) 
    print ("word found")
text = 'this is a test'
words =['image', 'is']

found = False
found_words = []

for k in words:
    if k in text:
        found_words.append(k)
        found = True

if found:
    print("words found:", found_words)
else:
    print("nope")
将打印:

words found: ['is']
试试这个:

text = 'this is a test'
words =['image', 'is']

for k in [w for w in words if w in text]: 
    print (k) 
    print ("word found")
text = 'this is a test'
words =['image', 'is']

found = False
found_words = []

for k in words:
    if k in text:
        found_words.append(k)
        found = True

if found:
    print("words found:", found_words)
else:
    print("nope")
将打印:

words found: ['is']
试试这个

for i in words:
    if i in text:
        print(i)
试试这个

for i in words:
    if i in text:
        print(i)

删除break for loop感谢编辑yatu,我试图找出如何格式化问题中的代码。删除break不起作用,它只是继续运行,直到它打印出单词列表中的每个工作。这与中断无关,整个循环都是错误的,它只是打印
单词列表。删除break for loop感谢编辑yatu,我试图找出如何格式化问题中的代码。删除break不起作用,它只是继续运行,直到它打印出单词列表中的每个工作。这与中断无关,整个循环都是错误的,它只是打印单词列表。它对@Darrenweatherall有帮助吗?别忘了投票/接受,谢谢!!这对Darrenweatherall有帮助吗?别忘了投票/接受,谢谢!!