Assigning to lists with arbitrary names

Magnus Lie Hetland mlh at idi.ntnu.no
Wed Oct 10 20:32:40 CEST 2001


"Stephen Boulet" <stephen.boulet at motorola.com> wrote in message
news:3BC35131.C9AB1393 at motorola.com...
> I can't quite figure out how to do this.
>
> I have 100 text files in a directory: file00.txt to file99.txt.
>
> I want to read each file in turn, parse it, and generate a list from
> each file.
>
> 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  :).

Excuse me if this isn't helpful, but -- why do you want to avoid a list of
lists of lists? And -- if each file generates a list, and you have a list
of files -- wouldn't that simply be a list of lists? (I can see the
problem with naming the list if lists "list", since that is the name
of the built-in type/function list... :)

> Any idea how I can do this? Thanks.

Certainly -- just use eval(), or exec or access globals() or something.
I don't see why you think having something named foo42 is better than
foo[42], though, since you will no longer be able to iterate over all
of your foos, and you will have to hand-code everything... But here
is an example of how it could be done (untested):

for number in range(100):
    exec 'list%s = process("file%s.txt")' % (number, number)

Again: I would not advise you to actually use this -- I see no reason to,
and I see several reasons not to :)

> -- Stephen

--

  Magnus Lie Hetland         http://www.hetland.org

 "Reality is that which, when you stop believing in
  it, doesn't go away."           -- Philip K. Dick






More information about the Python-list mailing list