简体   繁体   English

"访问列表列表中每个列表的第 n 个元素"

[英]Access n:th element of every list in list of lists

I have a problem in python and one step I need to do is to access the second element of every list in a list of lists.我在 python 中有一个问题,我需要做的一个步骤是访问列表列表中每个列表的第二个元素。 The list is:名单是:

[(0, 'Gallery', 'PROPN', 'nsubj'), (1, 'unveils', 'VERB', 'root'), (2, 'interactive', 'ADJ', 'amod')]

[(0, 'A', 'DET' 'det'), (1 'Christmas' , 'PROPN', 'compound'), (2, 'tree' ,'NOUN', 'nsubjpass')]

when you are enumerating over the list in the for loop:当您在 for 循环中枚举列表时:

for word in words:
    pass // do something

word= [[(0, 'Gallery', 'PROPN', 'nsubj'), (1, 'unveils', 'VERB', 'root'), (2, 'interactive', 'ADJ', 'amod')],[(0, 'A', 'DET' 'det'), (1,'Christmas' , 'PROPN', 'compound'), (2, 'tree' ,'NOUN', 'nsubjpass')]] word= [[(0, 'Gallery', 'PROPN', 'nsubj'), (1, 'unveils', 'VERB', 'root'), (2, 'interactive', 'ADJ', 'amod' )],[(0, 'A', 'DET' 'det'), (1,'Christmas' , 'PROPN', 'compound'), (2, 'tree' ,'NOUN', 'nsubjpass') ]]

[j[2] for i in word for j in i] ==> Outputs ['PROPN', 'VERB', 'ADJ', 'DETdet', 'PROPN', 'NOUN'] [j[2] for i in word for j in i] ==> 输出 ['PROPN', 'VERB', 'ADJ', 'DETdet', 'PROPN', 'NOUN']

[j[1] for i in word for j in i] ==> Outputs ['Gallery', 'unveils', 'interactive', 'A', 'Christmas', 'tree'] [j[1] for i in word for j in i] ==> 输出 ['Gallery', 'unveils', 'interactive', 'A', 'Christmas', 'tree']

you should try:你应该试试:

all_my_lists= [
    [(0, 'Gallery', 'PROPN', 'nsubj'), (1, 'unveils', 'VERB', 'root'), (2, 'interactive', 'ADJ', 'amod')],
    [(0, 'A', 'DET' 'det'), (1, 'Christmas' , 'PROPN', 'compound'), (2, 'tree' ,'NOUN', 'nsubjpass')]
]

for my_list in all_my_lists:
    for tup in my_list:
        print(tup[2])

Try this code:试试这个代码:

`for sub_list_temp in list: `对于列表中的 sub_list_temp:

for temp_tuple in sub_list_temp:

    print(temp_tuple[2])

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM