如何在Sympy中查找表达式中的所有术语 [英] How to find all terms in an expression in Sympy

查看:33
本文介绍了如何在Sympy中查找表达式中的所有术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表达式:1/(x+1)+4*x/(x-1)+3-4*x**2+10*x**2

我需要的是一个包含这个表达式中的术语的列表.
[1/(x+1), 4*x/(x-1), 3, -4*x**2 , 10*x**2]

更新:它不应该收集类似的术语.因此,在收集类似术语后,该列表应分别具有 -4*x** 2 和 10*x** 2 而不是 6*x**2.

解决方案

根据问题和评论,如果您可以将表达式作为字符串获取,那么如果您想避免术语收集,您可以这样做.

>

(sympify("1/(x+1)+4*x/(x-1)+3-4*x**2+10*x**2",evaluate=False)).args

这将返回所有条款而不收集类似条款.

I have an expression as: 1/(x+1)+4*x/(x-1)+3-4*x**2+10*x**2

What I need is a list that contain the terms in this expression.
i.e. [1/(x+1), 4*x/(x-1), 3, -4*x**2 , 10*x**2]

update: It should not collect like terms. Therefore the list should have -4*x** 2 and 10*x** 2 separately and not 6*x**2 after collecting like terms.

解决方案

Based on the question and comments, if you can get the expression as a string, you can do something like this if you want to avoid term collection.

(sympify("1/(x+1)+4*x/(x-1)+3-4*x**2+10*x**2", evaluate=False)).args

this will return all the terms without collecting like terms.

这篇关于如何在Sympy中查找表达式中的所有术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆