This function returns a list of n-tuples from a single "flat" list.
1 2 3 4 5 6 7 8 9 10 | def group(lst, n):
"""group([0,3,4,10,2,3], 2) => [(0,3), (4,10), (2,3)]
Group a list into consecutive n-tuples. Incomplete tuples are
discarded e.g.
>>> group(range(10), 3)
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
"""
return zip(*[lst[i::n] for i in range(n)])
|
There is probably an easier technique that accomplishes the same thing but I've found this function useful in XML data processing.
Tags: algorithms
Solution using a generator. You can do this with a generator and avoid creating the intermediate lists:
Requires input to be a sequence rather than just an iterable. This requires that the input be a sequence rather than just an iterable. For a generator-based version that can take any iterable (and which doesn't discard incomplete tail items) see my own recipe at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303279.
Speed comparison. There is a speed vs memory vs flexibility trade-off that needs to be made here (along with the correct semantics regarding the final incomplete tuple). Here are my timings for the 4 functions that have been presented (group2 and batch2 are the alternate implementations suggested in comments). As you can see, I've skewed the test in favor of the iteration implementations by using a fairly large input list and starting with an iterable input.
In the code that I designed this function for [len(lst) ~= 1000, list input], my algorithm is more than 10x faster than the next fastest alternative.
Fastest iterable version.
Now with code!
For more arbitrary reshaping of a sequence, see here .
We can use zip function, the list in conjunction with itself
1-dimension array ---> 2-dimension array
1-dimension array ---> 3-dimension array