|
2
|
Split up a sequence in same-size(if possible) parts.
As discussed on: http://gumuz.looze.net/wordpress/index.php/archives/2005/04/20/python-chopping-up-a-sequence/ It's very straight forward:
It takes a list, re-groups them in another list, 2 items per group. The source list doesn't have to be evenly dividable though:
Of course, a string is a sequence too. That's what I used it for last time. I had a string of bits, which I needed to divide in pieces of 8, bytes:
Tags: algorithms
|
4 comments
Add a comment
Sign in to comment
Download
Copy to clipboard

One issue with this recipe is that you wind up with a list at the end that could be much shorter than what you have. Consider this alternative:
This produces the following results, which may be preferred for some applications:
very useful as well. Thanx Nick, that's a cool one as well. Do you want me to add your version to the recipe or are you going to add this as a recipe as well?
Cheers, Guyon Morée http://gumuz.looze.net/
I just submitted it as a separate recipe, though I'm not sure I like it so much. I wonder if it could be done using only integer math.
There's variations on this: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303279 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/347689 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303060