如何获得函数是函数的变量列表?

时间:2019-06-04 07:11:08

标签: python python-3.x sympy

假设我有:

[
 {
  "title": "New Product",
  "row_data": [
   {
    "type": "category",
    "data": [{_id: 5cf4edb7c70d4716d821d45c, image: "image.jpg", title: "Saree"},{_id: 5cf4edcdc70d4716d821d45d, image: "image.jpg", title: "Kurti"}]
   },
   {
    "type": "banner",
    "data": [{_id: 5ce4eb55b02bd01ca09eb909, name: "first banner", image: "banner-image.jpg"}]
   },
   {
    "type": "product",
    "data": [{_id: 5cf4eed8c70d4716d821d465, image: "image.jpg", sku: "S-01"},{_id: 5cf4fa09c70d4716d821d483, image: "image.jpg", sku: "K-01"}]
   }
  ],
  "_id": "5cf611c7fcc98b16b0e89200"
 }
]

如何从import sympy as sp theta = sp.S('theta') u = sp.S('u') X = sp.Function('X')(theta,u) 中获取包含变量Xu的列表? 因此:theta产生X.SomeMethodOrFunc()

2 个答案:

答案 0 :(得分:1)

X.free_symbols将做您想要的,但是您需要将结果集转换为列表:

>>> import sympy as sp
>>> theta = sp.S('theta')
>>> u = sp.S('u')
>>> X = sp.Function('X')(theta,u)
>>> X.free_symbols
{theta, u}
>>> list(X.free_symbols)
[theta, u]

答案 1 :(得分:1)

尽管free_symbols有效,但是更直接的请求是.args,它将(对于具有它们的任何SymPy对象)返回该对象中包含的对象。对于函数,参数实际上就是您所看到的:

>>> Function('f')(x,y).args
(x, y)

对于其他事情,可能还会有更多退货

>>> IndexedBase('A')[x,y].free_symbols
{A, A[x, y], x, y}