[Tutor] create 1000 000 variables

Dave Kuhlman dkuhlman at rexx.com
Sat Jul 15 20:40:38 CEST 2006


On Fri, Jul 14, 2006 at 09:25:57PM -0400, Etienne Robillard wrote:
> 
> --- ÁÕàÓöÙ <kyxaxa at gmail.com> wrote:
> 
> > suppose I need to create 1000 000 variables
> > var_1, var_2, .... var_1000000
> > 
> > how to do this using for?
> > (something like
> > for i in range(1000000):
> > ___var_
> 
> 
> How would you consider NOT creating 100,000 variables
> ??
> 
> Answer that question and you should be close of
> knowing
> the answer to your own question.
> 

I'm with Etienne.  You probably need analysis, not a mechanism for
a solution that you've picked too quickly.

However, if a look-up by name *is* what you need, also consider
dictionaries.  After all, in Python, variables are just a
dictionary look-up in a namespace.

    In [1]: d1 = {}
    In [2]: for idx in range(10):
       ...:     key = 'k%d' % idx
       ...:     d1[key] = idx * 10
       ...:
       ...:
    In [3]: d1
    Out[3]:
    {'k0': 0,
     'k1': 10,
     'k2': 20,
     'k3': 30,
     'k4': 40,
     'k5': 50,
     'k6': 60,
     'k7': 70,
     'k8': 80,
     'k9': 90}


> HTH,
> 

Me, too.

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman


More information about the Tutor mailing list