Python-如果字符串与字符串不匹配,则仅打印一次 [英] Python - Print out only once if string do not match a string

查看:123
本文介绍了Python-如果字符串与字符串不匹配,则仅打印一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试制作一个比较脚本,基本上在该脚本中进行匹配字符串的打印,然后打印出来,否则仅打印其中不包含的字符串。

So I am trying to make a comparing script where I basically make if there is matching string then print it out else just print out that it doesn't contain.

我现在的问题是,每当包含字符串时,它只打印一次,这很好,但是每当找不到匹配项时,它就会打印出很多不匹配的字符串。

My problem right now is that whenever it contains the string it print outs only once which is good but whenever it doesn't find a matching, it prints out alot of not matched strings.

etc:

for words in stackoverflow:

   word_random = words #random meaning every loop
   #"Hello my name is Python and the World is so amazing What"
   #My name is Python and it is amazing!



    Matching_keyword = ['Hello', 'World', 'What']

    for keyword in Matching_keyword:
         if keyword in word_random:
            print(keyword)

         else:
             print(keyword)

    Output:

    Hello
    World
    What
    Hello
    World
    What
    Hello
    World
    What
    Hello
    World
    What
    Hello
    World
    What
    Hello
    World
    What
    Hello
    World
    What
    Hello
    World
    ....
    ....
    ....

输出几乎永远不会结束,并且比这里输出的输出长很多。如您所见,我在顶部有一个for循环,它经过的每个循环都赋予了新的含义,在比较之后,我就知道了。

The output almost never ends and is alot longer than what the ouput is printed here. As you can see I have at the top a for loop which every loop it goes through it gives a new meaning which I there after compare.

我的问题是我该如何这样,只要它碰到 true ,就只应该打印一次关键字,而 else 语句也要打印出来?

My question is how can I make it so whenever it hits the true it should only print out the keyword once and same goes to the else statement?

我尝试使用break,但是这样会杀死Matching_keyword循环并仅输出Hello,但会输出很多次

I tried to use break but that kills the Matching_keyword loop and prints out only Hello but alot of times

for keyword in Matching_keyword:
    if keyword in word_random:
       print("Found matching" + keyword)
       break
    else:
       print("Did not find matching")
       break


推荐答案

在这种情况下,您想在循环中使用 else ,而不是有条件的。

This is a case where you want to use else with the loop, not the conditional.

for keyword in Matching_keyword:
    if keyword in word_random:
       print("Found matching" + keyword)
       break
else:
    print("Did not find matching")

其他 else 子句仅在循环终止时才执行,因为您已经用完了迭代器。如果您以 break 或一个异常退出,则 else 不会执行。因此,在这里,一旦找到匹配项,就使用 break 停止检查其他关键字,也避免使用 else 。如果您从未找到匹配项,则循环会自然地终止(并且不会产生任何输出),并且 else 子句会打印失败消息。

An else clause on a loop only executes if the loop terminates because you've exhausted the iterator. If you exit with break or an exception instead, the else does not execute. So here, as soon as you find a match, you use break to stop checking other keywords, and you also avoid the else. If you never find a match, the loop terminates "naturally" (and without producing any output), and the else clause prints the failure message.

这篇关于Python-如果字符串与字符串不匹配,则仅打印一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆