Assigning to lists with arbitrary names
tanzer at swing.co.at
Thu Oct 11 08:57:02 CEST 2001
Cliff Wells <logiplexsoftware at earthlink.net> wrote:
> On Wednesday 10 October 2001 09:55, Daniel Dittmar wrote:
> > > Here's the problem: I want to name each list list00 to list99. I don't
> > > want to do list to list (since each list is actually a list of
> > > lists, and I'd like to avoid a list of lists of lists :).
> > You could use a dictionary with keys 'list00' to 'list99', but I don't
> > think having a dictionary of lists of lists is that much better. Why not
> > subclass UserList for each kind of list? Then you could at least
> > distinguish between the different levels.
> If you really _must_ have multiple variable names (IMHO a bad idea, since
> later you will have to reference all those lists and hence hardwire those
> names into your code which will make extensiblity murder - I still feel the
> dictionary method I recommended earlier is a simpler and more extensible
> solution) you could use something like:
> filenames = ["text01.txt", "text02.txt", ....] # use fnmatch or glob to get
> # the filenames in
> # real life
> i = 0
> for filename in filenames:
> listname = "list%02d" % i
> exec("%s = MyImporter('%s')" % (listname, filename))
> i += 1
> Which will give you 100 lists with the names list00, list99, etc and a ton of
> maintenence problems.
And it still uses a dictionary (just slightly hidden from your view)
so what's the point of such a gross hack?
I think the OP should consider getting rid of his fear of nested data
structures -- in Python they are your friend.
Christian Tanzer tanzer at swing.co.at
Glasauergasse 32 Tel: +43 1 876 62 36
A-1130 Vienna, Austria Fax: +43 1 877 66 92
More information about the Python-list