Welcome, guest | Sign In | My Account | Store | Cart
```def reshape(seq, how):
"""Reshape the sequence according to the template in ``how``.

Examples
========

>>> from sympy.utilities import reshape
>>> seq = range(1, 9)

>>> reshape(seq, ) # lists of 4
[[1, 2, 3, 4], [5, 6, 7, 8]]

>>> reshape(seq, (4,)) # tuples of 4
[(1, 2, 3, 4), (5, 6, 7, 8)]

>>> reshape(seq, (2, 2)) # tuples of 4
[(1, 2, 3, 4), (5, 6, 7, 8)]

>>> reshape(seq, (2, )) # (i, i, [i, i])
[(1, 2, [3, 4]), (5, 6, [7, 8])]

>>> reshape(seq, ((2,), )) # etc....
[((1, 2), [3, 4]), ((5, 6), [7, 8])]

>>> reshape(seq, (1, , 1))
[(1, [2, 3], 4), (5, [6, 7], 8)]

>>> reshape(tuple(seq), ([, 1, (2,)],))
(([, 2, (3, 4)],), ([, 6, (7, 8)],))

>>> reshape(tuple(seq), (, 1, (2,)))
((, 2, (3, 4)), (, 6, (7, 8)))

>>> reshape(range(12), [2, [3, set()], (1, (3,), 1)])
[[0, 1, [2, 3, 4, set([5, 6])], (7, (8, 9, 10), 11)]]

"""
m = sum(flatten(how))
n, rem = divmod(len(seq), m)
if m < 0 or rem:
raise ValueError('template must sum to positive number '
'that divides the length of the sequence')
i = 0
how_type = type(how)
rv = [None]*n
for k in range(len(rv)):
rv[k] = []
for hi in how:
if type(hi) is int:
rv[k].extend(seq[i: i + hi])
i += hi
else:
n = sum(flatten(hi))
hi_type = type(hi)
rv[k].append(hi_type(reshape(seq[i: i + n], hi)))
i += n
rv[k] = how_type(rv[k])
return type(seq)(rv)
```

#### Diff to Previous Revision

```--- revision 3 2012-10-25 20:14:59
+++ revision 4 2012-11-02 04:01:06
@@ -41,7 +41,7 @@
raise ValueError('template must sum to positive number '
'that divides the length of the sequence')
i = 0
-    container = type(how)
+    how_type = type(how)
rv = [None]*n
for k in range(len(rv)):
rv[k] = []
@@ -51,8 +51,8 @@
i += hi
else:
n = sum(flatten(hi))
-                fg = type(hi)
-                rv[k].append(fg(reshape(seq[i: i + n], hi)))
+                hi_type = type(hi)
+                rv[k].append(hi_type(reshape(seq[i: i + n], hi)))
i += n
-        rv[k] = container(rv[k])
+        rv[k] = how_type(rv[k])
return type(seq)(rv)
```