0
g_combine = [g1**4, g1**3*g2, g1**2 * g2**2, g1 * g2**3, g2**4]



def get_combine(g_combine, g1, g2):

return g_combine

for example, if i let user input a list of variables with the given formula g_combine of two variables, is there a way i can plug in the user inputted variables into g_combine and get the values of the output list based on the formula of g_combine? for example, if g1=1, g2=0, how do i get the output of [1, 0, ...0]

I was trying to say if user inputs all g_combine, g1, and g2, is there a way to let output the desired list based on g1 and g2 without me knowing the formula at the first place? Thanks!

Zhe Wang
  • 3
  • 2
  • 1
    just change `return g_combine` to `return [g1**4, g1**3*g2, g1**2 * g2**2, g1 * g2**3, g2**4]` – Nick Jun 29 '22 at 03:55
  • But I was trying to apply these values without me knowing it, that i can't implement it in the function beforehead. Like some other users defined it, they can just plug in the functions and values and get the result, someone else defined the list of that g_combine, I mean – Zhe Wang Jun 29 '22 at 03:58
  • @ZheWang: How are you supposed to apply it without knowing the formula? If the formula isn't known until later, define `get_combine` later, once you know the formula. There's no meaningful way to call it before you know the formula anyway. – ShadowRanger Jun 29 '22 at 04:10
  • You will need to use functions to represent the formulas; then the linked duplicate explains all you need. Functions in Python are first-class objects; they can be stored in lists, retrieved from lists, assigned to variables (i.e., *given new names*), and called from there. – Karl Knechtel Jun 29 '22 at 04:12
  • @ShadowRanger as I understood it, the goal is for the *contents of the list* to *represent* multiple formulae. This is trivial in Python by using higher-order functions, as explained in the duplicate I chose. – Karl Knechtel Jun 29 '22 at 04:12
  • Thank you all for replying, I think I didn't explain well here, The answer by Ash Nazg is what I mean. I was trying to say I do not know the formulas, and that list of formulas could change arbitrarily and it should be the user/results from other function as input. – Zhe Wang Jun 29 '22 at 04:25

1 Answers1

-1

If you pass each list element as string, you can do something like this:

def get_combine(g_combine, g1, g2):
    for i,x in enumerate(g_combine):
        g_combine[i]=eval(x)
    return g_combine

g_combine = ['g1**4', 'g1**3*g2', 'g1**2 * g2**2', 'g1 * g2**3', 'g2**4']
print(get_combine(g_combine, 1, 2))

Output:

[1, 2, 4, 8, 16]
Ash Nazg
  • 514
  • 3
  • 6
  • 14
  • Hi, I got a follow up question, if I choose to call get_combine repeatedly in a function with changing values of g1 and g2, it will always output the same with the correct output corresponding to the first inputs. For example, for i in range(1,4): print(get_combine(g_combine, i, 2)) will output three[1,2,4,8,16],which shouldn't be correct – Zhe Wang Jun 29 '22 at 04:49
  • Cz, that was actually replacing the formula list with output result. You can create a new list and append results to it. g_combine_output=[] for i in g_combine: g_combine_output.append(eval(i)) return g_combine_output – Ash Nazg Jun 29 '22 at 05:01