简体   繁体   中英

how to find the element using index method from a list

im working with a log file containing data. I converted the text file into a list using python, i would now like to print out the user that are mentioned in the file by using the index() method.

file = open("live.txt", 'r')
result = [line.split(' ') for line in file.readlines()]
result.index(10)

I know that the user is mention in element number 10 from the list, but for some reason i dont get the username printed out.

If you want an element from a list and you know its index, don't use the index method, use a subscript (AKA square brackets).

>>> file = open("live.txt", 'r')
>>> result = [line.split(' ') for line in file.readlines()]
>>> print(result[10])
['Hello,', "I'm", 'the', 'text', 'that', 'lives', 'on', 'the', 'eleventh', 'line', 'of', 'live.txt']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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