I have an issue with the creation of lists.
I have a function that returns the roots of a polynomial (see below). What I obtain is a list of roots (R.keys()
) and the times each one appears in the solution (R.values()
).
When I obtain R.items()
I am given a set of pairs of roots and their multiplicities: [(-2, 2), (-1, 1), (-3, 3)]
as returned from the example below.
But what I want is to obtain a list with each root repeated by the number of times it appears, that is, [-2, -2, -1, -3, -3, -3]
.
I guess this is not difficult but I'm getting stuck with finding a solution.
pol=Lambda((y), y**6 + 14*y**5 + 80*y**4 + 238*y**3 + 387*y**2 + 324*y + 108)
poli=Poly(pol(y))
R=roots(poli)
R.keys()
R.values()
R.items()
def list_of_roots(poli):
return(R.items())
list_of_roots(poli)