Home > Software design >  How to turn this Matrix into a list of integers?
How to turn this Matrix into a list of integers?

Time:12-06

I am trying to extract a list of integers from a input that looks like this:

[[matrix([[0.57863575]])], [matrix([[0.57170157]])], [matrix([[0.44320711]])], [matrix([[0.37195535]])]]

I am trying to get an output like so:

[0.57863575,0.57170157,0.44320711,0.37195535]

What are my options?

CodePudding user response:

You can use a loop comprehension:

from numpy import matrix
l = [[matrix([[0.57863575]])], [matrix([[0.57170157]])], [matrix([[0.44320711]])], [matrix([[0.37195535]])]]

[e[0].flat[0] for e in l]

output: [0.57863575, 0.57170157, 0.44320711, 0.37195535]

The real question is, how did you get this format in the first place? It might be better to fix it there.

  • Related