expanding string variables

David Bolen db3l at fitlinxx.com
Wed May 24 17:00:50 EDT 2000


"Michaell Taylor" <Michaell.Taylor at reis.com> writes:

> dict_set_list = {"first second ... seven"}
> set = string.splitfield(dict_set_list)
> for group in set:
>       group = group1[index]*group2[index]...etc
> 
> (I loop over 'index' elsewhere) That is, I would like for the term "group"
> to be expanded to first, second, ...seven, Thus
> allowing python to process:
> 
>       first = first1[index]*first2[index]...etc
>       second = second1[index]*second2[index]..etc
>       ...
>       seven = seven1[index]*seven2[index]...etc

Taking your problem as stated, one high level approach to
doing what you want would be to just build up the actual command you
want executed as a string and then dynamically execute it with the
'exec' statement.

So for example, your for loop could be written as:

    for group in set:
        exec "%s = %s1[index] * %s2[index]" % (group,group,group)

and you would then have local entries for "first", "second",
etc.. with your result values.  For example, the first pass through
the loop the execution string would become:

     "first = first1[index] * first2[index]"

This is just one example of building the string - there are any number
of ways to do so, so a better one might suit depending on the
operation you are actually coding (e.g., you might build the array
name strings once and reuse if you want to use them in multiple
expressions)

But, if possible, you might also want to revisit your data structure
to see if you can't use a multi-dimensional approach of some sort
rather than a naming convention.

For example, you have 7 sets of 20 dictionaries.  Why not just use a
list of lists of dictionaries.  Then you could just index into them
however you wanted.  I'm wondering if the other language you're coming
from perhaps forced the flattening of data structures and thus
provided some more convenient constructs for manipulating the name of
the data itself rather than the structure.

For example, rather than 

> first1 = {'firstelement': 45 ....., 'fifty': 89}
> first2 = {...}
> ...
> first20 = {...}
> second1 = {...}
> ...
> second20 = {...}

why not structure it as:

  dataset = [ [ {'firstelement':45 ...},     # first1
                {...},                       # first2
                ... ],
              [ {...},                       # second1
                ... ],
              ... ]

You wouldn't have to initialize it explicitly like this - starting
dataset off as an empty list ([]) and appending or setting elements as
you construct the data is just as easy.

And then your loop could become:

    for group in range(7):
        dataset[group][1][index] * dataset[group][2][index]

and so on.  dataset[0-7] would be equivalent to first-seven, and the
second list index the equivalent of the "1"-"20" suffix you have on
your names, and then it's a normal dictionary access.

If you wish to maintain name access (e.g., "first" rather than [0]),
just make it a dictionary of dictionaries instead of a list.
              
Using a more complex data structure generally lends itself more easily
to construction and computation, although as shown above, if you
really want to use variable naming the way you have, Python's ability
to dynamically execute arbitrary code fragments as strings can solve
that as well.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list