-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.

6
  • do you or don't you have all the obj1, obj2, ..., objn in a list? Commented May 17, 2015 at 5:51
  • I forgot to add a link to this stackoverflow.com/questions/8530694/… 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]. Commented May 17, 2015 at 6: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 Commented May 17, 2015 at 6: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. Commented May 17, 2015 at 7:29
  • 1
    I don't see why this justifies not using a list ... Commented May 17, 2015 at 7:46

3 Answers 3

1
sol_iterator = (fun(x) for x in obj_iterator)
Sign up to request clarification or add additional context in comments.

Comments

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]

Comments

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)))

6 Comments

Thank you, this is partly what I'm looking for. But in this way i don't have sol1,sol2, etc.
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())
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.
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!
@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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.