KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Suppose I have a matrix composed of a list of lists like so: >>> LoL=[list(range(10)) for i in range(10)] >>> LoL [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] Assume, also, that I have a numpy matrix of the same structure called LoLa : >>> LoLa=np.array(LoL) Using numpy, I could get a submatrix of this matrix like this: >>> LoLa[1:4,2:5] array([[2, 3, 4], [2, 3, 4], [2, 3, 4]]) I can replicate the numpy matrix slice in pure Python like so: >>> r=(1,4) >>> s=(2,5) >>> [LoL[i][s[0]:s[1]] for i in range(len(LoL))][r[0]:r[1]] [[2, 3, 4], [2, 3, 4], [2, 3, 4]] Which is not the easiest thing in the world to read nor the most efficient :-) Question: Is there an easier way (in pure Python) to slice an arbitrary matrix as a sub matrix?
Tags (comma-separated)
Save Edits
Cancel