Python - 如果包含列表中元组的元素,则仅打印行

标签 python list loops text

我有以下文本文件:

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

我该怎么做?

谢谢

最佳答案

您可以尝试组合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")

关于Python - 如果包含列表中元组的元素,则仅打印行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55576489/

相关文章:

list - Perl 6 中任何类型的列表是什么类型?

python - 追加到维度未知的嵌套列表

javascript - Vue循环嵌套JSON麻烦

linux - 如何用Tcl/Tk语言创建一个带有文本模式控件的定时器

python - 将数组转换为列表时出现数值错误

Python script.py 文件 url 参数

python - 为什么对 numpy 数组的就地修改性能与被修改的维度顺序有关?

python - 如何计算跨列列表中元素的平均值?

sql - SQL Server 2008 R2 有没有办法避免这种循环?

python - 访问存储在 pandas dataframe 中的数组