简体   繁体   English

在python中访问列表列表中的元素

[英]Access an element in a list of lists in python

I am new to python and am trying to access a single specific element in a list of lists. 我是python的新手,正在尝试访问列表中的单个特定元素。 I have tried: 我努力了:

line_list[2][0]

this one isn't right as its a tuple and the list only accepts integers. 这是不正确的,因为它是一个元组,并且列表仅接受整数。

line_list[(2, 0)]

line_list[2, 0]

This is probably really obvious but I just can't see it. 这可能确实很明显,但我看不到。

def rpd_truncate(map_ref):

    #Munipulate string in order to get the reference value
    with open (map_ref, "r") as reference:
        line_list = []
        for line in reference:
            word_list = []
            word_list.append(line[:-1].split("\t\t"))
            line_list.append(word_list)

    print line_list[2][0]

I get the exact same as if I used line_list[2]: 我得到的和我使用line_list [2]完全一样:

['Page_0', '0x00000000', '0x002DF8CD']

actually split will return a list more over you don't require word_list variable 实际拆分将返回更多list ,而不需要word_list变量

for line in reference:       
    line_list.append(line[:-1].split("\t\t"))
print line_list[2][0]

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

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