Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
91 views
in Technique[技术] by (71.8m points)

Is there a Python function for removing variables from a list?

I'm looking for a way to clean up this code. I'm using a curve_fit function to spit out fitted coefficients for various functions which have different numbers of parameters. I then want to use these fitted parameters to predict the outputs. The problem is, I'm using these clunky if/else statements to check how many parameters I need to feed into my function. I feel like there is an obvious way to achieve this much more cleanly but I just can't seem to see it.

fit = op.curve_fit(func, ordered_radii, ordered_masses, p0 = p0, bounds = bounds, maxfev = 10000000)[0]

if len(fit) == 1:
    predicted_masses = func(ordered_radii, fit[0])
    
elif len(fit) == 2:
    predicted_masses = func(sieve_radii, fit[0], fit[1])
    
elif len(fit) == 3:
    predicted_masses = func(ordered_radii, fit[0], fit[1], fit[2])
        
elif len(fit) == 4:
    predicted_masses = func(ordered_radii, fit[0], fit[1], fit[2], fit[3])

Thank you very much.

question from:https://stackoverflow.com/questions/66047931/is-there-a-python-function-for-removing-variables-from-a-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...