set variable to looping index?
mdekauwe at gmail.com
Wed Jul 29 18:29:30 EDT 2009
On Jul 29, 11:02 pm, "Rhodri James" <rho... at wildebst.demon.co.uk>
> On Wed, 29 Jul 2009 19:56:28 +0100, Martin <mdeka... at gmail.com> wrote:
> > Hi,
> > I am trying to set the return value from a function to a name which I
> > grab from the for loop. I can't work out how I can do this without
> > using an if statement...
> > for f in var1_fn, var2_fn, var3_fn:
> > if f.split('.') == 'var1':
> > var1 = call_some_function(f)
> > .
> > .
> > .
> > etc
> > Really I would like to remove the need for this if loop and I am sure
> > there is a simple way I am missing?
> It's a little hard to tell what you actually want from your description,
> but it looks like you're fighting the language unnecessarily here. If
> you have a sequence of functions that you want a sequence of results
> out of, you should be thinking in terms of a sequence type. A list,
> in other words.
> results = 
> for f in fn1, fn2, fn3:
> Rhodri James *-* Wildebeest Herder to the Masses
Thanks and apologises I wasn't trying to be cryptic, like all things
when I wrote it I thought it was quite transparent.
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). I will as suggested try the
dictionary option. I appreciate all the suggestions.
More information about the Python-list