两个列表+两个条件

时间:2018-03-01 16:39:55

标签: python

我正在寻找一种方法来连接两个列表和两个条件...我的方式不起作用,我不知道如何正确。

我想要的是什么:

在文件中查找触发器列表的术语。 如果已找到,请查找城市列表条款。 只有找到这种组合,才能打印成功

以下所有方法都不起作用......

triggerlist = ["hotel", "venue"]
citylist = ["austria", "germany", "switzerland", "spain"]

with open(filename, 'r') as myself:
    for word in words(myself):

    for i in triggerlist and citylist:
          if i in word:
              print ('SUCCESS')

    for i in triggerlist:
          if i in word and triggerlist:
              print ('SUCCESS')

    for i in triggerlist:
          if i in word 
             if i in triggerlist:
             print ('SUCCESS')

更新:

因为我似乎不够清楚(但我以为我是),这里是我想要的另一个简要说明:

textfromfile = ["A log line of text containing several words"]
triggerlist = ["hotel", "venue"]
citylist = ["austria", "germany", "switzerland", "spain"]

if word from triggerlist is in textfromfile:
    print 'nice'

if word from triggerlist AND word from citylist is in textfromfile:
    print 'what I want'

现在更好地理解我想要的东西吗?

4 个答案:

答案 0 :(得分:0)

如果您只想查找触发列表和城市列表中出现的单词,可以运行

triggerlist = ["hotel", "venue"]
citylist = ["hotel", "austria", "germany", "switzerland", "spain"]
word = ["hotel"]

for w in word:
    if w in triggerlist and w in citylist:
        print(w)
        print('SUCCESS')

另请注意,Python会懒惰地评估布尔条件 https://docs.python.org/3/reference/expressions.html#boolean-operations

答案 1 :(得分:0)

如果我理解你的问题,你是否想看看是否有任何触发列表单词也在城市列表中?如果是这种情况,您可以通过将它们转换为集合并查找交集来非常有效地执行此操作:

triggerlist = ["hotel", "venue"]
citylist = ["hotel", "austria", "germany", "switzerland", "spain"]
word = "hotel"

if word in set(triggerlist) & set(citylist):
    print('SUCCESS')

如果你正在阅读一个单词文件:(注意如果每行有多个单词,这个答案是不完整的,但你可以弄清楚其余的单词)。

triggerlist = ["hotel", "venue"]
citylist = ["hotel", "austria", "germany", "switzerland", "spain"]
intersection_set = set(triggerlist) & set(citylist)

with open(filename, 'r') as myself:
    for word in myself:
        if word in intersection_set:
            print('SUCCESS')

更新

鉴于新信息。您可以将触发器和城市列表转换为集合,以非常有效地比较重叠(&)。您可以将第二张支票放在第一张支票内,而不是独立执行。

triggers = set(["hotel", "venue"])
cities = set(["austria", "germany", "switzerland", "spain"])

with open('filename', 'r') as myfile:
    for line in myfile:
        word_set = set(line.split())
        if bool(word_set & triggers):
            print 'nice' # condition 1 satisfied
            if bool(word_set & cities):
                print 'what I want' # condition 1 and 2 satisfied

我还没有测试过这段代码,请注意,但要让它正常工作应该很简单。

答案 2 :(得分:0)

经过几个小时的思考和尝试,我自己找到了它。事实证明,这不是一个安排问题,而是简单的理解。解决方案是非常简单和基本的python东西。经过太多编码后,我认为这只是停电。也许这个答案对其他人也有帮助。

triggerlist = ["hotel", "venue"]
citylist = ["hotel", "austria", "germany", "switzerland", "spain"]


for i in triggerlist:
    if i in word:
        trigger = 1

for x in citylist:
    if x in word:
        city = 1

if trigger == 1 and city == 1:
    print 'SUCCESS'

答案 3 :(得分:0)

试试这个: -

textfromfile = ["A log line of text containing several words"]
triggerlist = ["hotel", "venue"]
citylist = ["austria", "germany", "switzerland", "spain"]
for i in triggerlist:
    if i in str(textfromfile):
        print("Nice")
for i in citylist:
    if i in str(textfromfile):
        if i in str(textfromfile):
            print("What i want")