ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Splitting up a sequence
Submitter: Guyon Morée (other recipes)
Last Updated: 2005/06/08
Version no: 1.3
Category: Algorithms

 

4 stars 1 vote(s)


Description:

Split up a sequence in same-size(if possible) parts.

Source: Text Source

# Guyon Morée
# http://gumuz.looze.net/

def split_seq(seq,size):
    """ Split up seq in pieces of size """
    return [seq[i:i+size] for i in range(0, len(seq), size)]

Discussion:

As discussed on: http://gumuz.looze.net/wordpress/index.php/archives/2005/04/20/python-chopping-up-a-sequence/

It's very straight forward:

>>> lst = [1,2,3,4,5,6,7,8,9,10]
>>> split_seq(lst,2)
[[1, 2], [3, 4], [5, 6], [7, 8], [9,10]]

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:

>>> lst = [1,2,3,4,5,6,7,8,9,10]
>>> split_seq(lst,3)
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]

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:

>>> bits = '101011101001011010010110'
>>> split_seq(bits,8)
['10101110', '10010110', '10010110']



Add comment

Number of comments: 4

Nick Matsakis, 2005/06/08
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:

def split_seq(seq, size):
	newseq = []
	splitsize = 1.0/size*len(seq)
	for i in range(size):
		newseq.append(seq[int(round(i*splitsize)):int(round((i+1)*splitsize))])
	return newseq
This produces the following results, which may be preferred for some applications:
>>> split_seq([1,2,3,4,5,6,7,8,9,10], 3)
[[1, 2, 3], [4, 5, 6, 7], [8, 9, 10]]

Add comment

very useful as well, Guyon Morée, 2005/06/09
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/
Add comment

Nick Matsakis, 2005/06/10
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.
Add comment

Gene tani, 2005/06/21
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
Add comment



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. Wrapping template engine ...

4. Assignment in expression

5. SOLVING THE METACLASS ...

6. Povray for python

7. Calling Windows API ...

8. Generic filter logic ...

9. Function Decorators by ...

10. MS SQL Server log monitor




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.