Assigning to lists with arbitrary names
Cliff Wells
logiplexsoftware at earthlink.net
Wed Oct 10 16:22:02 EDT 2001
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[0] to list[99] (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.
--
Cliff Wells
Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308
(800) 735-0555 x308
More information about the Python-list
mailing list