I am relatively new to SageMath and to Python. I have lists of objects of the form (k, A)
with k
an integer and A
a symbolic expression (in my case, it is a couple of monomials, but I would like to know the general answer if possible). For example, suppose I have:
l1 = [(2, A1), (1, A3)]
l2 = [(-1, A3), (3, A2)]
This is a very minimal example: in the cases I want to consider, I have many lists, each containing dozens of entries. But no list contain two elements with the same second element; for example, there is no list containing (1, A1)
and (2, A1)
.
I would like to extract the first component of corresponding symbolic expressions to have a matrix such that each line correspond to a list, and the columns are indexed by the A1, ..., An
that appears in at least one list. Also, the entry (i, j)
is the first component of (k, Aj)
in the list i
if it appears, and 0
otherwise.
In my example, I would like to have the matrix
[[2, 0, 1], [0, 3, -1]]
But I am unable to know how to do in general! I thought it should be like the f.coefficient_matrix()
of a polynomial f
, but the strategy of this algorithm distinguishes the elements using exponents, and in my case there are too many possibilities for the possible A
.
Is there a way to do this anyway?