Tuesday 15 February 2011

python - Sub matrix of a list of lists (without numpy) -


Suppose I have a matrix from a list of lists:

  & gt; & Gt; & Gt; For category = [category (category (10)) (10)]> gt; & Gt; & Gt; 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] [ Let me assume that I have a narrow matrix of the same structure, which is  0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]   

Lola :

  & gt; & Gt; & Gt; Using a sample, I can get a submatrix of this matrix in such a way:  
  & gt; & Gt; & Gt; Lola [1: 4,2: 5] array ([[2, 3, 4], [2, 3, 4], [2, 3, 4]])   

I The pattern matrix can make the piece as pure Python:

  & gt; & Gt; & Gt; R = (1,4)> gt; & Gt; & Gt; S = (2,5)> gt; & Gt; & Gt; [L [L] [R] [R] [1]] [[2, 3, 4], [2, 3, 4], [2, 3, 4]]   < P> Which is not the easiest thing to read and is not the most efficient: -)  

Question: Is there an easy way to piece an arbitrary matrix (pure Python) in the form of pure matrix? "post-text" itemprop = "text">

 [7] [7]: [[2, 3, 4], [2, 3, 4], [2] 2]  list :  
  By defining the subclass of the square you can copy the syntax of NumPy:   

List (list): def __init __ (self, * args): list .__ init __ (auto, * args) def __getitem __ (self, items): try: return list .__ getitem __ (auto, item) Excluding TypeError: Rows, Cols = item row [row] [for rows] [row]] lol = LL (for category in the list (category (10)) (10)] Print (y G [1: 4, 2: 5])

also yield

  [[2, 3, 4], [2, 3, 4] , [2, 3, 4]]   

Lol will not win any speed test using the subclass: [85] In: [time] [row] [2: 5] [1: 4] for the line in the X 1000000 loops, the best 3: 538 in the NS loop [82]:% Timeit lol [1: 4, 2: 5] 100000 loops, the best 3 : 3.07 per loop per

But speed is not everything - Sometimes readability is even more important.

No comments:

Post a Comment