Flatten nested lists#
- sage.misc.flatten.flatten(in_list, ltypes=(<class 'list'>, <class 'tuple'>), max_level=9223372036854775807)[source]#
Flatten a nested list.
INPUT:
in_list
– a list or tupleltypes
– optional list of particular types to flattenmax_level
– the maximum level to flatten
OUTPUT:
a flat list of the entries of
in_list
EXAMPLES:
sage: flatten([[1,1],[1],2]) [1, 1, 1, 2] sage: flatten([[1,2,3], (4,5), [[[1],[2]]]]) [1, 2, 3, 4, 5, 1, 2] sage: flatten([[1,2,3], (4,5), [[[1],[2]]]], max_level=1) [1, 2, 3, 4, 5, [[1], [2]]] sage: flatten([[[3],[]]],max_level=0) [[[3], []]] sage: flatten([[[3],[]]],max_level=1) [[3], []] sage: flatten([[[3],[]]],max_level=2) [3]
>>> from sage.all import * >>> flatten([[Integer(1),Integer(1)],[Integer(1)],Integer(2)]) [1, 1, 1, 2] >>> flatten([[Integer(1),Integer(2),Integer(3)], (Integer(4),Integer(5)), [[[Integer(1)],[Integer(2)]]]]) [1, 2, 3, 4, 5, 1, 2] >>> flatten([[Integer(1),Integer(2),Integer(3)], (Integer(4),Integer(5)), [[[Integer(1)],[Integer(2)]]]], max_level=Integer(1)) [1, 2, 3, 4, 5, [[1], [2]]] >>> flatten([[[Integer(3)],[]]],max_level=Integer(0)) [[[3], []]] >>> flatten([[[Integer(3)],[]]],max_level=Integer(1)) [[3], []] >>> flatten([[[Integer(3)],[]]],max_level=Integer(2)) [3]
In the following example, the vector is not flattened because it is not given in the
ltypes
input.sage: flatten((['Hi', 2, vector(QQ, [1,2,3])], (4,5,6))) # needs sage.modules ['Hi', 2, (1, 2, 3), 4, 5, 6]
>>> from sage.all import * >>> flatten((['Hi', Integer(2), vector(QQ, [Integer(1),Integer(2),Integer(3)])], (Integer(4),Integer(5),Integer(6)))) # needs sage.modules ['Hi', 2, (1, 2, 3), 4, 5, 6]
We give the vector type and then even the vector gets flattened:
sage: tV = sage.modules.vector_rational_dense.Vector_rational_dense # needs sage.modules sage: flatten((['Hi', 2, vector(QQ, [1,2,3])], (4,5,6)), # needs sage.modules ....: ltypes=(list, tuple, tV)) ['Hi', 2, 1, 2, 3, 4, 5, 6]
>>> from sage.all import * >>> tV = sage.modules.vector_rational_dense.Vector_rational_dense # needs sage.modules >>> flatten((['Hi', Integer(2), vector(QQ, [Integer(1),Integer(2),Integer(3)])], (Integer(4),Integer(5),Integer(6))), # needs sage.modules ... ltypes=(list, tuple, tV)) ['Hi', 2, 1, 2, 3, 4, 5, 6]
We flatten a finite field.
sage: flatten(GF(5)) [0, 1, 2, 3, 4] sage: flatten([GF(5)]) [Finite Field of size 5] sage: tGF = type(GF(5)) sage: flatten([GF(5)], ltypes=(list, tuple, tGF)) [0, 1, 2, 3, 4]
>>> from sage.all import * >>> flatten(GF(Integer(5))) [0, 1, 2, 3, 4] >>> flatten([GF(Integer(5))]) [Finite Field of size 5] >>> tGF = type(GF(Integer(5))) >>> flatten([GF(Integer(5))], ltypes=(list, tuple, tGF)) [0, 1, 2, 3, 4]
Degenerate cases:
sage: flatten([[],[]]) [] sage: flatten([[[]]]) []
>>> from sage.all import * >>> flatten([[],[]]) [] >>> flatten([[[]]]) []