python - Dynamically creating arrays for multiple datasets -
this quality of life query feel there answer to, can't find (maybe i'm using wrong terms)
essentially, have multiple sets of large data files perform analysis on. involves reading each of these datafiles , storing them array (of variable length).
so far have been doing
import numpy np input1 = np.genfromtxt('data1.dat') input2 = np.genfromtxt('data2.dat') etc. wondering if there method of dynamically assigning array each of these datasets. since can read these dynamically loop,
for in xrange(2): input = np.genfromtxt('data%i.dat'%i) i hoping combine above create bunch of arrays; input1, input2, etc. without myself typing out genfromtxt multiple times. surely there method if had 100 datasets (aptly named data0, data1, etc) import.
a solution can think of maybe creating function,
import numpy np def input(a): return np.genfromtxt('data%i.dat'%a) but obviously, prefer store in memory instead of regenerate list, , extremely grateful know if possible in python.
you can choose store arrays in either dict or list:
option 1
using dict.
data = {} in xrange(2): data['input{}'.format(i)] = np.genfromtxt('data{}.dat'.format(i)) you can access each array key.
option 2
using list.
data = [] in xrange(2): data.append(np.genfromtxt('data{}.dat'.format(i))) alternatively, using list comprehension:
data = [np.genfromtxt('data{}.dat'.format(i)) in xrange(2)] you can use map, returns list:
data = map(lambda x: np.genfromtxt('data{}.dat'.format(x)), xrange(2)) now can access each array index.
Comments
Post a Comment