Python: convert matrix of ints to matrix of Decimals

Question:

I have a Python matrix and due to rounding issues in later parts of the code I would like to convert it to a matrix of Decimal elements. I am using Python3. How do I do that?

My matrix is

a = [
    [0, 9, 1, 1],
    [0, 0, 1, 0],
    [0, 0, 0, 1],
    [1, 1, 1, 0]
]

Something like b = Decimal(a) which gives an error.

Asked By: SCBuergel

||

Answers:

Based on the great comment by Denis Rasulev this can be achieved by

b = [[Decimal(i) for i in j] for j in a]
Answered By: SCBuergel