Create matrix from list of list of tuples using list comprehension

Viewed 675

I have a list of lists of tuples list1

list1 = [[('a',0.01),('b',0.23),('c',1e-7)],
      [('a',0.91),('b',0.067),('c',0.38)]]

and I want to create a numpy matrix where each row would be the second value of a tuple in list1. So the matrix, lets call it A, would have form

A = [[0.01,0.23,1e-7],[0.91,0.067,0.38]]
A.shape
>>> (2,3)

So far I have managed to achieve this in a slow and inefficient way

A = []
for i in range(len(list1)):
    A.append(np.array([v for k,v in list1[i]]))
A = np.array(A)

How can I do this using list comprehension?

1 Answers
Related