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
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With