Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can create it using nested lists:</p> <pre><code>matrix = [[a,b],[c,d],[e,f]] </code></pre> <p>If it has to be dynamic it's more complicated, why not write a small class yourself?</p> <pre><code>class Matrix(object): def __init__(self, rows, columns, default=0): self.m = [] for i in range(rows): self.m.append([default for j in range(columns)]) def __getitem__(self, index): return self.m[index] </code></pre> <p>This can be used like this:</p> <pre><code>m = Matrix(10,5) m[3][6] = 7 print m[3][6] // -&gt; 7 </code></pre> <p>I'm sure one could implement it much more efficient. :)</p> <p>If you need multidimensional arrays you can either create an array and calculate the offset or you'd use arrays in arrays in arrays, which can be pretty bad for memory. (Could be faster though…) I've implemented the first idea like this:</p> <pre><code>class Matrix(object): def __init__(self, *dims): self._shortcuts = [i for i in self._create_shortcuts(dims)] self._li = [None] * (self._shortcuts.pop()) self._shortcuts.reverse() def _create_shortcuts(self, dims): dimList = list(dims) dimList.reverse() number = 1 yield 1 for i in dimList: number *= i yield number def _flat_index(self, index): if len(index) != len(self._shortcuts): raise TypeError() flatIndex = 0 for i, num in enumerate(index): flatIndex += num * self._shortcuts[i] return flatIndex def __getitem__(self, index): return self._li[self._flat_index(index)] def __setitem__(self, index, value): self._li[self._flat_index(index)] = value </code></pre> <p>Can be used like this:</p> <pre><code>m = Matrix(4,5,2,6) m[2,3,1,3] = 'x' m[2,3,1,3] // -&gt; 'x' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload