On this SO question
<https://stackoverflow.com/questions/60547540/how-to-get-coefficients-in-sympy-expression-python>
the following approach was posted:
>>> y = 2*x**3 + a*x**2 + b
>>> d = dict(i.as_independent(x)[::-1] for i in Add.make_args(y)); d
{1: b, x**3: 2, x**2: a}
You then can use as a key whichever power of x you are interested in; use
`d.get(1/x, 0)` to default to 0 if the value is not there.
On Friday, March 20, 2020 at 6:00:56 AM UTC-5, Jisoo Song wrote:
>
> I know there is 'coeff' method, which gives the coefficient of specific
> power of variable: e.g. (3/x + 4*x).coeff(x, -1) giving 3.
>
> Then, is there any method to give every coeffecient of powers of x? For
> example, {-1:3, 1:4} where keys are powers and values are coefficient.
>
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/ab7db708-3131-4943-a844-8b6a9881c99f%40googlegroups.com.