-2

I saw this question and I would like to know if is possible to use something to solve a problem I have.

I defined list of mathematical object

obj1,obj2,...,objn

And I have a function fun which does something with these objects

sol1=fun(obj1)
sol2=fun(obj2)
...
soln=fun(objn)

Now what I wanted is use a for loop to calculate all the sol1. I know I can do it putting all the object on a list but I was wondering if is possible do something else.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
rpanai
  • 12,515
  • 2
  • 42
  • 64
  • do you or don't you have all the obj1, obj2, ..., objn in a list? – Julien Spronck May 17 '15 at 05:51
  • I forgot to add a link to this http://stackoverflow.com/questions/8530694/use-iterator-as-variable-name-in-python-loop I know I can do a it all the object on a list but I was wondering if there is another way to do it. For some reasons could be useful to me have all solutions as sol_i instead sol[i]. – rpanai May 17 '15 at 06:05
  • there are multiple ways to do what you're asking for (exec, globals, ...). However, these methods are usually highly frowned upon and using a list or a dict is lot more pythonic – Julien Spronck May 17 '15 at 06:24
  • Hi Julien, the objects are not (meant to be) on a list. I agree that using a list is more pythonic but as I need the k object to be objk and not obj[k] I'm looking for some possible alternative. This is due to the particular nature of the object obj. As example if I'm in a multidimensional space I can use x[0] as x_1 and x[n-1] as x_n but if i want to do a partial derivative I much prefer to have my variable called x_k than x[k-1]. I hope it is more clear. And sorry again to everyone for the bad edited question. – rpanai May 17 '15 at 07:29
  • 1
    I don't see why this justifies not using a list ... – Julien Spronck May 17 '15 at 07:46
  • @JulienSpronck: This is a math software written in python: http://doc.sagemath.org/html/en/prep/Quickstarts/Multivariable-Calculus.html#partial-differentiation and there is an example with 2 variables only. When you use more than 3 of them you name them x_1,x_2,...,x_n. There is no reasons that justifies using a list for x. I hope this clarify a little bit. – rpanai May 17 '15 at 08:19

3 Answers3

1
sol_iterator = (fun(x) for x in obj_iterator)
skolsuper
  • 629
  • 1
  • 6
  • 21
1

I defined list of mathematical object obj1,obj2,...,objn

So you have

objs = [obj1, obj2, obj3]

and want to create a list of the results?

The easiest thing is

sols = [fun(obj) for obj in objs]
glglgl
  • 89,107
  • 13
  • 149
  • 217
0

So you have:

obj1,obj2,...,objn 

and some function, let us say:

def func(obj):
    print(obj*2))

So, you are looking for this:

for i in range(1,n+1):
   fun(eval("obj"+str(i)))
  • Thank you, this is partly what I'm looking for. But in this way i don't have sol1,sol2, etc. – rpanai May 17 '15 at 06:19
  • Well this answer gets you most of the way there. Possibly you need to use `exec` rather than eval to create variables with variable names, e.g. `for i in range(1,n+1): exec('sol{0} = fun(obj{0})'.format(str(i)), globals(), locals())` – skolsuper May 17 '15 at 06:30
  • 1
    Disclaimer: While the above may work (I didn't test it), whatever you are really trying to achieve, this is not the best way to do it. – skolsuper May 17 '15 at 06:33
  • 1
    just change the _print_ on the function for a return, and use the `exec` command to asign "sol"+str(index) to each call of the function. I agree with @skolsuper this is not a good way to to this! – eavildosola May 17 '15 at 06:33
  • @skolsuper: you can see the discussion with Julien above. Thanks for your answer it works for fun with one input. But what if I have a fun(obj_i, b) where b is given and not changing over the loop? – rpanai May 17 '15 at 08:25
  • `exec` gets passed your global and local variables, so you can just change the string to `fun(obj{0}, b)`, the interpreter should resolve b with no problem – skolsuper May 17 '15 at 08:40