I have something like this:
[e for e in ([n for n in xrange(random.randrange(1, 5))] for x in xrange(10))]
It produces:
[[0, 1, 2, 3], [0, 1, 2], [0], [0], [0, 1], [0], [0, 1], [0, 1, 2, 3], [0, 1, 2], [0, 1, 2]]
And I need the same but in flat structure.
For now I use something like:
l = []
[l.extend(e) for e in ([n for n in xrange(random.randrange(1, 5))] for x in xrange(10))]
But is there something less obsucre to achieve this 'unpacking' of arbitrary length list inside comprehension?
-
dupplicate of stackoverflow.com/questions/716477/join-list-of-lists-in-pythonStephane Rolland– Stephane Rolland2013年04月07日 13:55:04 +00:00Commented Apr 7, 2013 at 13:55
4 Answers 4
Use this list comprehension:
In [8]: [y for x in xrange(10) for y in xrange(random.randrange(1, 5))]
Out[8]: [0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 3, 0, 0, 1, 0]
The above list comprehension is equivalent to this(but LC are much faster):
In [9]: lis=[]
In [10]: for x in xrange(10):
....: for y in xrange(random.randrange(1, 5)):
....: lis.append(y)
....:
5 Comments
for y in xrange(random.randrange(1, 5)) iterates over that arbitrary lenght list to return a flattened list.The best way to flatten any iterable in a generic situation is itertools.chain.from_iterable():
>>> import random
>>> from itertools import chain
>>> x = [e for e in ([n for n in xrange(random.randrange(1, 5))]
... for x in xrange(10))]
>>> list(chain.from_iterable(x))
[0, 0, 0, 1, 2, 3, 0, 1, 2, 3, 0, 0, 1, 2, 3, 0, 1, 0, 1, 0, 0, 1, 2]
This said, it's preferable to avoid the extra work in this case by just making it flat to begin with.
Comments
You can use numpy flatten():
import numpy as np
l = [e for e in ([n for n in xrange(random.randrange(1, 5))] for x in xrange(10))]
a = np.asarray(l)
l = list(a.flatten(l))
print l
Comments
import itertools
l = [e for e in ([n for n in xrange(random.randrange(1, 5))] for x in xrange(10))]
result = list(itertools.chain(*l))
and then print result gives:
[0,1,2,3,0,1,2,0...]
the use of the * in chain(*l) is inspired from this question join list of lists in python .
1 Comment
itertools.chain.from_iterable() that was designed to do this more efficiently.