Reputation: 429
I have a file:
I split the lines so I get this:
But I'm only interested in the last item in each of those lists, so 77, 77, 6 etc. Is there a way to obtain them?
Upvotes: 0
Views: 715
Reputation: 19743
if you are working with big file:
>>> with open('yourfile') as f:
... last_items = [ int(x.strip().split()[-1]) for i,x in enumerate(f) if i> 1 ]
...
Upvotes: 0
Reputation: 4468
For each item, take its [-1] element. Index [-1] refers to the last element of an array, [-2] to the next-to-last, etc.
Upvotes: 0
Reputation: 729
Assuming you already have that array,
lastItems = [int(i[-1]) for i in yourList[2:]]
Upvotes: 2