set variable to looping index?

Martin mdekauwe at gmail.com
Wed Jul 29 19:04:59 EDT 2009


On Jul 29, 11:57 pm, "Jan Kaliszewski" <z... at chopin.edu.pl> wrote:
> 30-07-2009 <mdeka... at gmail.com> wrote:
> > All I was trying to do was call a function and return the result to an
> > a variable. I could admittedly of just done...
>
> > var1 = some_function(var1_fn)
> > var2 = some_function(var2_fn)
> > var3 = some_function(var3_fn)
>
> > where var1_fn, var2_fn, var3_fn are just filenames, e.g. var1_fn =
> > 'x.txt'. But I figured I would try and make it slightly more generic
> > whilst I was at it, hence my attempt to use the filenames to create
> > the variable names (hence the loop).
>
> Hi,
>
> Then you could also consider using simply lists:
>
> filenames = p'foo', 'bar', baz']
> results = []
> for name in filenames:
>      results.append(some_function(name))
>
> If filenames were generated according to a particular pattern, you can
> mimic that pattern and generate filenames list using
> list-comprehension, e.g.:
>
> filenames = ['file{nr}.txt'.format(nr=nr) for nr in range(13)]
>
> Chreers,
>
> *j
>
> --
> Jan Kaliszewski (zuo) <z... at chopin.edu.pl>

I guess I wanted to keep the function returns in separate arrays in
this case, hence my attempt to make variable names based on the
filenames.

Thanks

Martin



More information about the Python-list mailing list