我有一个文本文件,每行有3个数字。
我还有一个列表编号,例如:self
我喜欢在文本文件中找到所有3个数字来自列表的行。例如:
文本文件:
lists = [1,2,3,4,5,6]
我想找到这一行:11 20 6
3 5 1
30 20 12
最快的方法是什么?
答案 0 :(得分:1)
使用split()
和set()
:
l = [1,2,3,4,5,6]
with open('data.txt') as file:
for i, line in enumerate(file):
if(set(list(map(int, line.split()))).issubset(l)):
print("Line %d has all numbers from the list" % i)
使用示例文件:data.txt
,如下所示:
11 20 6
3 5 1
30 20 12
<强>输出:强>
Line 1 has all numbers from the list