I have this program:
import sys
import itertools
from itertools import islice
fileLocation = input("Input the file location of ScoreBoard: ")
input1 = open(fileLocation, "rt")
amountOfLines = 0
for line in open('input1.txt').readlines( ):
amountOfLines += 1
timestamps = [line.split(' ', 1)[0][0:] for line in islice(input1, 2, amountOfLines)]
teamids = [line.split(' ', 1)[0][0:] for line in islice(input1, 2, amountOfLines)]
print(teamids)
and this text file:
1
5 6
1 5 1 5 0
1 4 1 4 1
2 1 2 1 1
2 2 3 1 1
3 5 2 1 1
4 4 5 4 1
For teamids, I want it to start reading after the first space and to the next space, starting from the second line which, I have already achieved but don't get how to start reading after the first space to the next. For timestamps i have managed this but only starting from the first character to the first space and don't know how to do this for teamids. Much help would be appreciated
Here's one suggestion showcasing a nice use case of zip
to transpose your array:
lines = open(fileLocation, 'r').readlines()[2:]
array = [[int(x) for x in line.split()] for line in lines]
transpose = list(zip(*filter(None, array)))
# now we can do this:
timestamps = transpose[0] # (1, 1, 2, 2, 3, 4)
teamids = transpose[1] # (5, 4, 1, 2, 5, 4)
This exploits the fact that zip(*some_list)
returns the transpose of some_list
.
Beware of the fact that the number of columns you get will be equal to the length of the shortest row. Which is one reason why I included the call to filter
to remove empty rows caused by empty lines.