473,573 Members | 3,208 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to iterate through a sequence, grabbing subsequences?

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
4 1867
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.String IO(long_string)
chunk = f.read(chunksiz e)
while chunk:
yield chunk
chunk = f.read(chunksiz e)
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
1374
by: Chris Wright | last post by:
Hi, 1) I want to iterate over a list "N at a time" sort of like: # Two at a time... won't work, obviously >>> for a, b in : .... print a,b ....
10
2981
by: vd12005 | last post by:
hello, i'm wondering how people from here handle this, as i often encounter something like: acc = # accumulator ;) for line in fileinput.input(): if condition(line): if acc: #1 doSomething(acc) #1
13
2397
by: stef mientki | last post by:
hello, I generate dynamically a sequence of values, but this "sequence" could also have length 1 or even length 0. So I get some line in the form of: line = '(2,3,4)' line = '' line = '(2)' (in fact these are not constant numbers, but all kind of integer
5
10690
by: ray | last post by:
A container object provides a method that returns an iterator object. I need to iterate the sequence with that iterator, but need to skip the first item. I can only iterate the whole sequence with: for x in container.iterChildren(): How to skip the first item? It seems that it's a simple question. Could somebody help me? Thanks.
1
7730
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5252
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3692
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2163
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1256
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
984
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.