Funzies
Funzies

Reputation: 129

Easier way to produce a list out of a nested list?

Using some numerical algorithm, my code produces a list of matrices, which is stored in a nested list, like follows

A = [matrix([[1,2],[3,4]]), matrix([[5,6],[7,8]]), ...)

Subsequently, I want to plot the values 1,5,9,... against some other list, say 'x', with the same length. At the moment I loop over the values I want like such

wanted_sol
for i in range(0,len(A))
   wanted_sol.append(A[i][0,0])

and then I plot 'wanted_sol'. I was wondering if there is a shorter way to do this? I tried several things like

plot(x, A[:][0,0])
plot(x, A[0:len(A)][0,0]),

but I cannot get it to work.

Upvotes: 0

Views: 46

Answers (1)

Andrey Sobolev
Andrey Sobolev

Reputation: 12693

You can convert A to numpy.ndarray and use numpy slice notation:

>>> A = np.array([np.matrix([[1,2],[3,4]]), np.matrix([[5,6],[7,8]])])
>>> A[:,0,0]
array([1, 5])

Upvotes: 2

Related Questions