【问题标题】:Python - Only print lines if contains the elements of a tuple from a ListPython - 如果包含列表中元组的元素,则仅打印行
【发布时间】:2019-04-08 15:07:23
【问题描述】:

我有以下文本文件:

We are playing football at World Cup
teste
We are playing football
Playing test 
World Cup Football

我只想提取包含 (World Cup and Football) 或 ('Playing', 'test') 的行。

例如,基于我的文本文件,我只想提取这个:

We are playing football at World Cup
Playing test 
World Cup Footbal

基本上我只想提取该行是否包含每个元组中的两个值。

为此,我正在尝试以下代码:

file = 'text.txt'
words = [('Football','World Cup'), ('Playing test ')]
with open(file, "r") as ins:
    for line in ins:
        if all(x in line.lower() for x in words):
            print(line)

但我的代码出现以下错误:

TypeError: 'in <string>' requires string as left operand, not tuple

我该怎么做?

谢谢

【问题讨论】:

  • 您的代码不明确。明确单词列表的第二个元素。是('playing test') 还是('playing', 'test')??

标签: python list loops text


【解决方案1】:

你可以试试anyall的组合:

if any(all(words.lower() in line.lower() for words in word_tuples) for word_tuples in words)

您可以检查单词列表中的任何内容以及列表中的所有项目。

(无文件测试)

# Note: second element needs to be tuple else causes unexpected results
words = [('Football','World Cup'), ('Playing test',)] 
ins = ["We are playing football at World Cup",
       "teste",
       "We are playing football",
       "Playing test",
       "World Cup Football"]

for line in ins:
    if any(all(words.lower() in line.lower() for words in word_tuples) for word_tuples in words):
        print(line)

输出:

We are playing football at World Cup
Playing test
World Cup Football

正如下面评论中提到的,如果第二个元素不是元组,则会导致意外结果。使用测试示例,以下显示错误,因为它正在比较所有字符是否相同而不是单词:

x = "test palying"
if all(w.lower() in x for w in words[1]):
    print("ERROR")

【讨论】:

  • 当单词的第二个元素不是元组时,您的代码如何工作。 op 的代码也有歧义,因为他讲述了关于第二个元素的两件事。
  • @Vicrobot 谢谢!我没有意识到这一点。我认为它会导致意外结果,因此,修复第二个元素也需要为 tuple,即 ('Playing test',)
【解决方案2】:

你真的很接近,你只需要再循环一次:

file = 'text.txt'
words = [('Football','World Cup'), ('Playing test ')]
with open(file, "r") as ins:
    for line in ins:
        for tup in words:
            if all(word.lower() in line.lower() for word in tup):
                print(line)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 2020-04-08
    • 2016-04-26
    相关资源
    最近更新 更多