Ask Your Question

Revision history [back]

Here is a one liner that might be a bit better since the numpy function tolist is optimized:

  sage: sum((m[i,i+1:].tolist() for i in range(4)), [])
  [1, 2, 3, 6, 7, 11]

Vincent

Here is a one liner that might be a bit better since the numpy function tolist is optimized:

  sage: sum((m[i,i+1:].tolist() for i in range(4)), [])
  [1, 2, 3, 6, 7, 11]

And if you want to be smarter with respect to memory allocation you can do

sage: l = []
sage: for i in range(4):
....:     l.extend(m[i,i+1:].tolist())
sage: l
[1, 2, 3, 6, 7, 11]

Vincent

Here is a one liner that might be a bit better since the numpy function tolist is optimized:

  sage: sum((m[i,i+1:].tolist() for i in range(4)), [])
  [1, 2, 3, 6, 7, 11]

And if you want to be smarter with respect to memory allocation you can do

sage: l = []
sage: for i in range(4):
....:     l.extend(m[i,i+1:].tolist())
sage: l
[1, 2, 3, 6, 7, 11]

Vincent