By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,095 Members | 1,586 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,095 IT Pros & Developers. It's quick & easy.

How to iterate through a sequence, grabbing subsequences?

P: n/a
I wrote a function that I suspect may already exist as a python builtin,
but I can't find it:

def chunkify(s, chunksize):
"Yield sequence s in chunks of size chunksize."
for i in range(0, len(s), chunksize):
yield s[i:i+chunksize]

I wrote this because I need to take a string of a really, really long
length and process 4000 bytes at a time.

Is there a better solution?

Matt

--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
Sep 29 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Matthew Wilson wrote:
I wrote a function that I suspect may already exist as a python builtin,
but I can't find it:

def chunkify(s, chunksize):
"Yield sequence s in chunks of size chunksize."
for i in range(0, len(s), chunksize):
yield s[i:i+chunksize]

I wrote this because I need to take a string of a really, really long
length and process 4000 bytes at a time.

Is there a better solution?
I don't know if it's better, but StringIO let you read a string as if it
was a file:

def chunkify(s, chunksize):
f = StringIO.StringIO(long_string)
chunk = f.read(chunksize)
while chunk:
yield chunk
chunk = f.read(chunksize)
f.close()

Now I'm sure someone will come up with a solution that's both far better
and much more obvious (at least if you're Dutch <g>)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Sep 29 '06 #2

P: n/a
def chunkify(s, chunksize):
"Yield sequence s in chunks of size chunksize."
for i in range(0, len(s), chunksize):
yield s[i:i+chunksize]

I wrote this because I need to take a string of a really, really long
length and process 4000 bytes at a time.

Is there a better solution?
My first thought, if len(s) is truely huge, would be to replace
range() with xrange() so that you don't build a list of
len(s)/chunksize elements, just to throw it away.

However, I think that's about as good as this common idiom gets.

I've seen variants which will always yield portions of chunksize
in size, padding it out to the proper length, but that's a
specification issue that you don't seem to want/need.

-tkc


Sep 29 '06 #3

P: n/a
Matthew Wilson wrote:
I wrote a function that I suspect may already exist as a python builtin,
but I can't find it:

def chunkify(s, chunksize):
"Yield sequence s in chunks of size chunksize."
for i in range(0, len(s), chunksize):
yield s[i:i+chunksize]

I wrote this because I need to take a string of a really, really long
length and process 4000 bytes at a time.

Is there a better solution?
what's wrong with your solution ?

</F>

Sep 29 '06 #4

P: n/a
Matthew Wilson wrote:
I wrote a function that I suspect may already exist as a python builtin,
but I can't find it:

def chunkify(s, chunksize):
"Yield sequence s in chunks of size chunksize."
for i in range(0, len(s), chunksize):
yield s[i:i+chunksize]

I wrote this because I need to take a string of a really, really long
length and process 4000 bytes at a time.

Is there a better solution?
There's not any builtin for this, but the same topic came up just three
days ago: http://tinyurl.com/qec2p.

Regards,
George

Sep 29 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.