I have a file with data that I want to split into different lists depending on the value in the first column. I know exactly what the possible values ββare in the first column, but they are not in any good mathematical progression. Is there a shorter way to write the following code?
x0=[] y0=[] x2=[] y2=[] x16=[] y16=[] # etc. for line in file: words=line.split() if words[0] == '0': x0.append(words[1]) y0.append(words[2]) elif words[0] == '2': x2.append(words[1]) y2.append(words[2]) elif words[0] == '16': x16.append(words[1]) y16.append(words[2]) # etc.
My thinking process is lower, but the strings (x and y) that I define are obviously strings and do not reference the lists that I want them to reference.
x0=[] y0=[] x2=[] y2=[] x16=[] y16=[] # etc for line in file: words=line.split() x='x'+words[0] y='y'+words[0] x.append(words[1]) y.append(words[2])
Update: I understand that this can be done with a dictionary where my meaning will be a list of lists, but basically I'm curious if there is a pythonic way to code this, which follows my thought movement, as stated above.
source share