Python| sympy.factor_list() 方法
借助sympy.factor_list()方法,我们可以在SymPy中以(factor, power)元组的形式得到一个数学表达式的因子列表。
Syntax: factor_list(expression)
Parameters:
expression – It is a mathematical expression.
Returns: Returns a list of factors of the given mathematical expression in the form of (factor, power) tuple.
示例 #1:
在这个例子中,我们可以看到通过使用sympy.factor_list()方法,我们可以获得给定数学表达式的因子列表。
# import sympy
from sympy import * x = symbols('x')
expr = x**2 * z + 4 * x*y * z + 4 * y**2 * z
print("Mathematical expression : {}".format(expr))
# Use sympy.factor_list() method
f = factor_list(expr)
print("List of factors : {}".format(f))
输出:
Mathematical expression : x**2*z + 4*x*y*z + 4*y**2*z
List of factors : (1, [(z, 1), (x + 2*y, 2)])
示例 #2:
# import sympy
from sympy import * x = symbols('x')
expr = (x**2 - 2 * x + 1)
print("Mathematical expression : {}".format(expr))
# Use sympy.factor_list() method
f = factor_list(expr)
print("List of factors : {}".format(f))
输出:
Mathematical expression : x**2 - 2*x + 1
List of factors : (1, [(x - 1, 2)])