Sage <= 8.9 was based on Python 2 in which zip
would return lists.
Sage >= 9.0 is based on Python 3 in which zip
returns "zip objects".
Zip objects are iterable, so list
can turn them into lists.
Example (from another Ask Sage question):
sage: a = [250, 770, 360, 190, 230, -1, 0, 0, 0, 0, 0, 1, 31,
....: 44, 14, 27, 3, 0, 480, 1770, 800, 580, 160, 0, 1,
....: 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
....: 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1]
sage: b = list(zip(*[iter(a)]*6))
sage: b
[(250, 770, 360, 190, 230, -1),
(0, 0, 0, 0, 0, 1),
(31, 44, 14, 27, 3, 0),
(480, 1770, 800, 580, 160, 0),
(1, 0, 0, 0, 0, 0),
(0, 1, 0, 0, 0, 0),
(0, 0, 1, 0, 0, 0),
(0, 0, 0, 1, 0, 0),
(0, 0, 0, 0, 1, 0),
(0, 0, 0, 0, 0, 1)]
Here we could also use matrix
:
sage: m = matrix(10, 6, a)
sage: m
[ 250 770 360 190 230 -1]
[ 0 0 0 0 0 1]
[ 31 44 14 27 3 0]
[ 480 1770 800 580 160 0]
[ 1 0 0 0 0 0]
[ 0 1 0 0 0 0]
[ 0 0 1 0 0 0]
[ 0 0 0 1 0 0]
[ 0 0 0 0 1 0]
[ 0 0 0 0 0 1]
Then we can get a list of rows:
sage: m.rows()
[(250, 770, 360, 190, 230, -1),
(0, 0, 0, 0, 0, 1),
(31, 44, 14, 27, 3, 0),
(480, 1770, 800, 580, 160, 0),
(1, 0, 0, 0, 0, 0),
(0, 1, 0, 0, 0, 0),
(0, 0, 1, 0, 0, 0),
(0, 0, 0, 1, 0, 0),
(0, 0, 0, 0, 1, 0),
(0, 0, 0, 0, 0, 1)]
The rows are really vectors (which display exactly as tuples).
To get actual tuples:
sage: [tuple(row) for row in m]
[(250, 770, 360, 190, 230, -1),
(0, 0, 0, 0, 0, 1),
(31, 44, 14, 27, 3, 0),
(480, 1770, 800, 580, 160, 0),
(1, 0, 0, 0, 0, 0),
(0, 1, 0, 0, 0, 0),
(0, 0, 1, 0, 0, 0),
(0, 0, 0, 1, 0, 0),
(0, 0, 0, 0, 1, 0),
(0, 0, 0, 0, 0, 1)]
Try
list
More precisely: in Python 2, zip would return a list. Nowadays in Python 3 it returns a zip object, which is an iterable, that can be turned into a list with
list
, i.e.list(zip(*[iter(s)]*n))
will give the corresponding list.thanks. all of the posts I saw online about this was ten years back.
I expanded the comments into an answer with an example and an alternative solution.
This way the question can appear as answered in the list of questions.