Python: How to evaluate a function which is string?

前端 未结 5 1958
悲&欢浪女
悲&欢浪女 2021-01-16 23:22

I get some models from database as

f(t)=(2.128795454425367)+(208.54359721863273)*t+(26.098128487929266)*t^2+(3.34369909584111)*t^3+(-0.3450228278737971)*t^4+         


        
5条回答
  •  清酒与你
    2021-01-16 23:50

    You can store the function as a python expersion in your database and when you get the string just do something like eval(funcstr.replace('x', 'yvalue')).

    To show you an example:

    funcstr = '2*x+5'
    evalpoint = funcstr.replace('x', '5')
    val = eval(funcstr)
    

    At this point val should be evaluated to 15

提交回复
热议问题