4

I am trying to make 100 lists with names such as: list1, list2, list3, etc. Essentially what I would like to do is below (although I know it doesn't work I am just not sure why).

num_lists=100

while i < num_lists:
    intial_pressure_{}.format(i) = []
    centerline_temperature_{}.format(i) = []

And then I want to loop through each list inserting data from a file but I am unsure how I can have the name of the list change in that loop. Since I know this won't work.

while i < num_lists:
    initial_pressure_i[0] = value

I'm sure what I'm trying to do is really easy, but my experience with python is only a couple of days. Any help is appreciated. Thanks

Greg
  • 63
  • 7
  • 3
    Why do you want to do this? How about a list of a hundred lists? – squiguy Apr 29 '13 at 00:57
  • dictionary of lists if key(well, list names are) is distinct. But again, what's the motivation? What exactly are you trying to solve? A lot of files, a lot of strings, a lot of lists will be memory monster. – CppLearner Apr 29 '13 at 01:01
  • 2
    Just make a dictionary of lists. – Mr_Spock Apr 29 '13 at 01:01
  • I am sure what I want to do can be done in a MUCH simpler way. But right now I have a file with odd numbers corresponding to pressure and even to temperature. There are a hundred of these files. I want to find the maximum pressure and temperature in each file, so I figured it would be easiest to make 200 lists from these files. Then in the end find the mean and std of the maximum values. – Greg Apr 29 '13 at 01:01
  • The thing is, do you need to read them in at once? Can you read each odd file and even file to find the max? In the future, if the files are huge, you need to look at generator. For now, okay, that's fine. – CppLearner Apr 29 '13 at 01:03
  • They are only 98 lines each. I wasn't sure if I would be able to just read in the file and find a max, since each file contains both pressure and temperature. – Greg Apr 29 '13 at 01:07
  • you could make lists inside lists and whatnot, but honeslty, your best bet is to just read odd lines to 1 list and even lines to another list using a `for` loop. – Ryan Saxe Apr 29 '13 at 01:16
  • Yeah I can do that once, but I need to do it 100 times with 100 different files that have names such as data1.txt, data2.txt and so forth, and keep the data separate from one another. – Greg Apr 29 '13 at 01:20
  • write a for loop that will read all the files into one big list, but a multidimensional array so that you can access all the rows in each file with list_name[x]. if all of your files have a similar name like data1, data2, data3 etc, you can open them and read them into the list based on their name since they have a progressional pattern. Are they like that? Please update your question. – Ryan Saxe Apr 29 '13 at 01:28

3 Answers3

5

Instead of creating 100 list variables, you can create 100 lists inside of a list. Just do:

list_of_lists = [[] for _ in xrange(100)]

Then, you can access lists on your list by doing:

list_of_lists[0] = some_value  # First list
list_of_lists[1] = some_other_value  # Second list
# ... and so on
juliomalegria
  • 24,229
  • 14
  • 73
  • 89
2

Welcome to Python!

Reading your comments on what you are trying to do, I suggest ditching your current approach. Select an easier data structure to work with.

Suppose you have a list of files:

files = ['data1.txt', 'data2.txt',...,'dataN.txt']

Now you can loop over those files in turn:

data = {}
for file in files:
   data[file] = {}
   with open(file,'r') as f:
      lines=[int(line.strip()) for line in f]
      data[file]['temps'] = lines[::2]            #even lines just read
      data[file]['pressures'] = lines[1::2]       #odd lines

Then you will have a dict of dict of lists like so:

 {'data1.txt': {'temps': [1, 2, 3,...], 'pressures': [1,2,3,...]},
  'data2.txt': {'temps': [x,y,z,...], 'pressures': [...]},
  ...}

Then you can get your maxes like so:

max(data['data1.txt']['temps'])

Just so you can see what the data will look like, run this:

data = {}
for i in range(100):
    item = 'file' + str(i)
    data[item] = {}
    kind_like_file_of_nums = [float(x) for x in range(10)]
    data[item]['temps'] = kind_like_file_of_nums[0::2]
    data[item]['pres'] = kind_like_file_of_nums[1::2]

print(data)
juliomalegria
  • 24,229
  • 14
  • 73
  • 89
dawg
  • 98,345
  • 23
  • 131
  • 206
  • First off thank you for helping me solve my problem. Do I have to specify the name of each file like you did in this line: files=['data1.txt', 'data2.txt',...,'dataN.txt'] – Greg Apr 29 '13 at 02:01
  • You will need to file name to open it, won't you? Or are you using stdin? Regardless, you need something hashable and distinct that maps to each each set of data to each file. – dawg Apr 29 '13 at 02:03
  • Okay so then I will have 100 entires in files =[] term? Also, I am getting an error on this line: lines=[int(line.strip()) for line in f] that says "ValueError: invalid literal for int() with base 10:" – Greg Apr 29 '13 at 02:06
  • 2
    You have something other than '123' or '565' for example on a line of text. Try `float` instead of `int` if you have '123.454' for example – dawg Apr 29 '13 at 02:08
  • Okay thanks. So I can find the max of each of the 200 "lists" I will have, using what you wrote. I'm assuming I can then just store each max in a list for temp and pres respectively then find the mean and std of each? – Greg Apr 29 '13 at 02:18
  • 1
    @Greg you are asking for things that are not in your question on your comments. If you want people to respond to include ways that will work for that, in your case it is how to open 100 files and save them in different places, you really need to include that. This way will work once you have those files set, but you asked your question as if you had already loaded your files into lists... – Ryan Saxe Apr 29 '13 at 02:25
  • 1
    To answer Greg's first question about the names of the files, you probably won't have to type the filenames out by hand. if there's some consistent pattern to them, then use the `glob` module (see http://docs.python.org/2/library/glob.html for details) to grab, say, all the filenames that match `data*.txt`. – rmunn Apr 29 '13 at 02:27
  • Yeah, I apologize for that. I wasn't really 100% sure what my question was. It kind of became more apparent as I got some assistance. Without a doubt I could have been much more clear. – Greg Apr 29 '13 at 02:28
  • 1
    Note that `glob` won't sort its results, so if you want them sorted then something like `results = list(sorted(glob.glob('data*.txt')))` should do the trick. – rmunn Apr 29 '13 at 02:28
  • And thanks a lot rmunn, that should save me a lot of time haha. – Greg Apr 29 '13 at 02:29
  • Do they need to be sorted? – Greg Apr 29 '13 at 02:32
  • It does not matter if you sort them since you lose sorted order when putting them in a dict. Sort them when coming out of the dict if you need them sorted for presentation... – dawg Apr 29 '13 at 03:46
1

You could just make a dictionary of lists. Here's an example found in a similar thread:

>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> for i in a:
...   for j in range(int(i), int(i) + 2):
...     d[j].append(i)
...
>>> d
defaultdict(<type 'list'>, {1: ['1'], 2: ['1', '2'], 3: ['2']})
>>> d.items()
[(1, ['1']), (2, ['1', '2']), (3, ['2'])]
Community
  • 1
  • 1
Mr_Spock
  • 3,815
  • 6
  • 25
  • 33