[英]Access tuple elemens in a nested list python
I'm new to python, I have a list with a nested list and inside it contains tuples it looks like below我是 python 的新手,我有一个带有嵌套列表的列表,其中包含如下所示的元组
[('dad', 'mom', 'test1.txt')]
[('brother', 'sister', 'test2.txt')]
[('uncle', 'aunty', 'test3.txt')]
[('grandpa', 'grandma', 'test4.txt')]
I would like to access one list at a time, for example, I open test1.txt file from 1st list index[2] and if the string 'dad' and 'mom' are present then filter the file and read it likewise for text2.txt file if 'brother' and 'sister' are there then read and filter.我想一次访问一个列表,例如,我从第一个列表索引 [2]打开 test1.txt 文件,如果存在字符串'dad' 和 'mom'然后过滤文件并同样读取 text2 .txt 文件,如果存在“兄弟”和“姐妹” ,则读取并过滤。
I have below code:我有以下代码:
for data in list:
file= data[2]
text_file= open(file,'r').readlines()
if data[0], data[1] in text_file:
#do something..
The above code is not giving me a proper output as per the requirement.上面的代码没有按照要求给我一个合适的 output 。 Can kindly someone help me with this approach?有人可以帮助我采用这种方法吗?
Thanks!谢谢!
This is how I have done it:这就是我的做法:
lis=[[('dad', 'mom', 'test1.txt')],
[('brother', 'sister', 'test2.txt')],
[('uncle', 'aunty', 'test3.txt')],
[('grandpa', 'grandma', 'test4.txt')]]
for i in range(len(lis)):
for j in lis[j]:
f = open(i[2])
lines = f.read()
if i[0] in lines and i[1] in lines:
#do sth
I have iterated the elements of the list in the range of the length of the list: len(lis)
.我已经迭代了列表长度范围内的列表元素length of the list: len(lis)
。 This way j
takes the values 0, 1, 2, 3
.这样j
取值0, 1, 2, 3
。 Then I have used a for loop
which iterates through the elements of the list.然后我使用了一个遍历列表元素的for loop
。 Hence, j
assumes values of the elements of lis
as lis[0], lis[1], lis[2], lis[3]
.因此, j
假定 lis 的元素值为lis
lis[0], lis[1], lis[2], lis[3]
。
Now that j
has values of the list elements, we can use i[(integer)]
and access the elements of the elements of the list ie, "dad", "mom", "test1.txt"
, etc.现在j
具有列表元素的值,我们可以使用i[(integer)]
并访问列表元素的元素,即"dad", "mom", "test1.txt"
等。
Hence we can apply conditions to i[0] or i[1]
etc.因此我们可以将条件应用于i[0] or i[1]
等。
For example:例如:
if i[0] == "dad":
print(something)
Hope this helps you!希望这对你有帮助!
Is not clear what's wrong for you since you read the correct file name 4 times由于您阅读了正确的文件名 4 次,因此不清楚您有什么问题
I suppose you have to exit the for loop if you find what you are looking for with break statement.我想如果你用 break 语句找到你要找的东西,你必须退出 for 循环。
Try not to use keywords like list
尽量不要使用list
之类的关键字
L=[[('dad', 'mom', 'test1.txt')],
[('brother', 'sister', 'test2.txt')],
[('uncle', 'aunty', 'test3.txt')],
[('grandpa', 'grandma', 'test4.txt')]]
for data in L:
f=data[0][2]
file1=open(f,"r")
text_file=file1.read()
if(data[0][0] and data[0][1] in text_file):
#do something
lis=[[('dad', 'mom', 'test1.txt')],
[('brother', 'sister', 'test2.txt')],
[('uncle', 'aunty', 'test3.txt')],
[('grandpa', 'grandma', 'test4.txt')]]
for i in lis:
data=i[0][2]
f=open(data,'r')
file=f.read()
if (i[0][0] and i[0][1])in file:
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.