Python Forum
How to use list of symbols for sympy calculation in python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use list of symbols for sympy calculation in python?
#1
Here is my simple code to store string elements ['a','b'] in a list L as sympy symbols. As output, I have new list L with two Symbols [a,b] in it. But when I try to use these symbols in my calculation I get an error saying a & b are not defined. Any suggestions on how can I fix this?

Basically, what I want to do is use string elements in a list as symbols for sympy calculations. Any suggestions on other methods to do this are welcomed. Thank you.

    
import sympy as sm
    L=['a','b']
    print(L)
    for j in range(len(L)):
        L[j] = sm.symbols(L[j])
Reply
#2
Such tricks are usually not recommended but you could try
>>> import sympy as sm
>>> L = ['a', 'b', 'c']
>>> globals().update(zip(L, sm.symbols(L)))
>>> a * a
a**2
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python sympy problem for symbolic equation definition joellapointe_engineering 0 87 Mar-24-2024, 11:09 PM
Last Post: joellapointe_engineering
  Sympy nonlinsolve does not return explicit solution axelle 1 2,194 Jan-20-2021, 11:38 AM
Last Post: Serafim
  plot differential equation sympy dan_adi 0 2,930 Oct-13-2020, 10:44 AM
Last Post: dan_adi
  Attribute error - Sympy VictorG8 1 4,893 Apr-09-2020, 06:03 PM
Last Post: snippsat
  Next zero of a function on sympy VicenteM 0 2,054 Aug-21-2019, 04:58 PM
Last Post: VicenteM
  Question #1: Count symbols aapurdel 3 4,481 May-29-2019, 02:49 AM
Last Post: heiner55
  sympy psosmol 9 4,897 Mar-11-2019, 11:09 PM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020