Python;如果行中的单词和单词2不一致

时间:2016-07-27 04:49:29

标签: python if-statement

抱歉在这里成为一个菜鸟,因为我是这个新手

我一直在尝试编写一个代码,该代码可以在行中找到某些单词,然后从第二个单词列表中匹配,以查看第二个单词是否存在于同一行中。 仅当第一个单词不存在于一行时才打印行;这是片段和示例

keywords =("a","b","c","d")
keywords2 = ("e","f","g")
a = ("a  is a ", "a is not e","b is b", "b is not f")
for key2 in keywords2:              
    for key in keywords:                

        for line in a:

                if key2 not in line:
                    if key in line:

                        print(key+" Found in --> ")
                        print      (line)

所需的输出是

a is a
b is b

虽然我们有

a Found in --> 
a  is a 
b Found in --> 
b is b
b Found in --> 
b is not f
a Found in --> 
a  is a 
a Found in --> 
a is not e
b Found in --> 
b is b
a Found in --> 
a  is a 
a Found in --> 
a is not e
b Found in --> 
b is b
b Found in --> 
b is not f

我已经尝试了几种方法来实现循环,但没有用

5 个答案:

答案 0 :(得分:2)

如果我理解正确,您只希望“行”的值为keywords,而不是keywords2。在这种情况下,只需:

for line in a:
    if [x for x in keywords if x in line] and not [x for x in keywords2 if x in line]:
        print line

[x for x in keywords if x in line]循环遍历keywords中的所有项目,如果它们位于line中,则会将其添加到列表中。

[x for x in keywords2 if x in line]循环遍历keywords2中的所有项目,如果它们位于line中,则会将其添加到列表中。

空列表被视为False,您希望keywords列表不为空,keywords2列表为空。

答案 1 :(得分:1)

@joon以你的问题风格回答答案;这里还有其他一些想法:

首先遍历,因为代码应该像“我应该打印此行吗?”这样的问题。对于每一行。

keywords = ("a","b","c","d")
keywords2 = ("e","f","g")

a = ("a  is a ", "a is not e", "b is b", "b is not f")

for line in a:
    for k in keywords:
        if k in line:
            for k2 in keywords2:
                if k2 in line:
                    break
            else:
                print(line)

在线试用:https://repl.it/CgW8

For / else是一个Python的想法,但它有点奇怪 - 如果循环正常结束,else:将运行并打印该行。如果循环中断,else:将不会运行,也不会打印该行。

但更惯用的答案是使用any(),如果列表中的任何内容为True,则返回True,否则返回False:

keywords =("a","b","c","d")
keywords2 = ("e","f","g")
a = ("a  is a ", "a is not e","b is b", "b is not f")

for line in a:
    if any(k in line for k in keywords):
        if not any(k2 in line for k2 in keywords2):
            print(line)

在线试用:https://repl.it/CgW9

答案 2 :(得分:0)

这对你有用吗?

keywords =("a","b","c","d")
keywords2 = ("e","f","g")
lines = ("a  is a ", "a is not e","b is b", "b is not f")

for keyword in keywords:
    for line in lines:
        if keyword in line:
            keyword2_exist = False
            for keyword2 in keywords2:
                if keyword2 in line:
                    keyword2_exist = True

            if not keyword2_exist:
                print(line)

答案 3 :(得分:0)

这可能对您更有效:

import re

keywords =("a","b","c","d")
keywords2 = ("e","f","g")

pattern = re.compile(r'^(\w+)\s+is\s+(?:not\s+)?(\w+)')
for line in ("a  is a", "a is not e","b is b", "b is not f"):
    for key1, key2 in pattern.findall(line):
        if key1 in keywords and key2 not in keywords2:
            print (line)

根据我的理解,字符串中有两个关键字;每个格式为key is [not] key2,您希望仅打印key位于keywordskey2不在keywords2的行。这基本上就是上述解决方案的作用

答案 4 :(得分:0)

你应该学习一些函数式编程,试试这个:

keywords =("a","b","c","d")
keywords2 = ("e","f","g")
a = ("a  is a ", "a is not e","b is b", "b is not f")

for line in a:
    if not any(map(lambda x: x in line, keywords2)):
        if any(map(lambda x: x in line, keywords)):
            print line