Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sympy subs() a list of values to evalue

I have:

import sympy as sp

L1,L2,T = sp.symbols("L1,L2,T")

expr = L1 + L2*T
expr.subs([(L1,2),(L2,4),(T,[1,2,3,4])])

I want to evaluate T at every value in the list [2,5,7,8] but it doesn’t evaluate the list. Is there a way to do this so that each value in the list can get evaluated and get an output for each? This is what I need:

 2+4*1 = 6
 2+4*2 = 10
 2+4*3 = 14
 2+4*4 = 18

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As pointed out by hpaulj:

res = [expr.subs([(L1,2),(L2,4),(T, t)]) for t in [1,2,3,4]]
res
# out: [6, 10, 14, 18]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading