0

Say I have a bunch of lists, let's call them x and y. They're coming from some data files, and those data files are numbered, say 1-36. Is there a way for me to go through each data file, and assign the number to x_1,y_1,x_2,y_2 as the variables?

For example:

def read_data(location):
    kx_input = []
    ky_input = []

    with open(location) as f:
        for line in f: 
            cols = line.split()
            kx_input.append(float(cols[0]))
            ky_input.append(float(cols[1]))

return kx_input,ky_input

for i in range(1,36):
    x,y = read_data('file_'+str(i)+'_data')

Except this redefines x and y each time, I want it to create an x_i,y_i with i being the value of i during each iteration. So in the end I would have x_1,y_1....x_36,y_36 as a bunch of lists.

To deal with this issue before I've made classes and appended all the x's and y's together and had a separate value that assigned them to a number, but I'm just wondering if it's possible to do it another way.

Wilsonwatson
  • 379
  • 1
  • 4
  • 13
  • Why not use a list of lists? Then to access `x_1` simply use `x_lists[0]` – Brobin Aug 03 '15 at 16:14
  • how do you plan to use such variables afterwards? i.e. why do you need them to be separate and named like that? Use a data structure to store them (e.g. a list, like mentioned above by @Brobin) – Pynchia Aug 03 '15 at 16:15
  • They are already lists, it is a bunch of lists, x and y are lists. That's why I was using classes before as essentially a list of these lists. I'm just wondering if I can make stand-alone lists where part of the variable definition comes from another variable – Wilsonwatson Aug 03 '15 at 16:17
  • answer my question above. Are you going to type their names out one by one afterwards? Not a good practice at all for a number of reasons. – Pynchia Aug 03 '15 at 16:18
  • well in my case they're coefficients that I'm using to create an equation to edit another list, not that that's very relevant. I suppose I would end up having to type them all out again, so it's not practical for this application. I was more curious for future applications if this was possible. I saw the link to the dictionaries thing and it doesn't seem like the same question. – Wilsonwatson Aug 03 '15 at 16:22

0 Answers0