【发布时间】:2020-06-16 09:17:08
【问题描述】:
我是 python 新手,我有一个带有嵌套列表的列表,其中包含如下所示的元组
[('dad', 'mom', 'test1.txt')]
[('brother', 'sister', 'test2.txt')]
[('uncle', 'aunty', 'test3.txt')]
[('grandpa', 'grandma', 'test4.txt')]
我想一次访问一个列表,例如,我从 1st list index[2] 打开 test1.txt 文件,如果字符串 'dad' 和 'mom ' 存在,然后过滤文件并同样读取 text2.txt 文件,如果 'brother' 和 'sister' 存在则读取并过滤。
我有以下代码:
for data in list:
file= data[2]
text_file= open(file,'r').readlines()
if data[0], data[1] in text_file:
#do something..
上面的代码没有按照要求给我正确的输出。有人可以帮助我使用这种方法吗?
谢谢!
【问题讨论】:
标签: python python-3.x list for-loop tuples