Re: [Tutor] Splitting a string into n-sized bytes

2006-03-15 Thread Kent Johnson
Steve Nelson wrote:
> Indeed - as I now have a function:
> 
> def nsplit(s, n):
>   return [s[i:i+n] for i in range(0, len(s), n)]
> 
> Incidentally I am currently going with:
> 
> def nsplit(s, n):
>   while s:
>yield s[:n]
>s = s[n:]

You can write the generator function to use the same method as the list 
comp and avoid creating all the intermediate (partial) lists:

def nsplit(s, n):
   for i in range(0, len(s), n):
 yield s[i:i+n]

One of the cool things about generators is that they make it so easy to 
maintain state between yields.

In Python 2.4, all you have to do is rewrite your original list comp to 
a generator comprehension:

def nsplit(s, n):
   return (s[i:i+n] for i in range(0, len(s), n))

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Splitting a string into n-sized bytes

2006-03-14 Thread Steve Nelson
On 3/14/06, Adam <[EMAIL PROTECTED]> wrote:

> Hopefully that should point you in the right direction to do n-sized
> words as well.

Indeed - as I now have a function:

def nsplit(s, n):
  return [s[i:i+n] for i in range(0, len(s), n)]

Incidentally I am currently going with:

def nsplit(s, n):
  while s:
   yield s[:n]
   s = s[n:]

As my friend just showed me how generators work, and also that the
beginning and end of lists are implicit.

Very cool.

Thanks for all your help!

S.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Splitting a string into n-sized bytes

2006-03-14 Thread Smith
| From: "Steve Nelson" 
| 
| Further to my previous puzzling, I've been working out the best way to
| chop a string up into n-sized words:
| 

I think the follow use of groupby is from Raymond Hettinger from ASPN recipes. 
The batch() function will return an iterable to you in user-definable sized 
sets.


from itertools import groupby
def batch(iterable, size):
def ticker(x, s=size, a=[-1]):
r = a[0] = a[0] + 1
return r // s
for k, g in groupby(iterable, ticker):
 yield g
s='this is my string to parse up.'
for i in batch(s,4):
print list(i)


The output is lists of (in this case) 4 characters; the last group is shorter, 
but you already know how to fix that.


['t', 'h', 'i', 's']
[' ', 'i', 's', ' ']
['m', 'y', ' ', 's']
['t', 'r', 'i', 'n']
['g', ' ', 't', 'o']
[' ', 'p', 'a', 'r']
['s', 'e', ' ', 'u']
['p', '.']


/c
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Splitting a string into n-sized bytes

2006-03-14 Thread Adam
On 14/03/06, Steve Nelson <[EMAIL PROTECTED]> wrote:
> Hello all,
>
> Further to my previous puzzling, I've been working out the best way to
> chop a string up into n-sized words:
>
> I'm aware that I can use a slice of the string, with, eg, myWord[0:4].
>
> I am also aware that I can do blob = myWord.pop(0) to take (and
> remove) the first character.  I can botch these together, eg
> myFourLetterWord =
> myWord.pop(0)+myWord.pop(0)+myWord.pop(0)+myWord.pop(0) but that is
> really horrid.
>
> I then tried doing something suitably silly, as follows:
>
> 1) Find the length of string.
> 2) Create a list using range(0, length, 4)
> 3) We now have the end points for each 'word', eg 4, 8, 12.
> 4) Now create a list of tuples that represent the slices we need, ie
> (0,4), (5,8) etc
> 5) Iterate over this list, grabbing the slices as depicted in the tuples.
> 6) Use these tuples to grab slices.
>
> The code looked like this:
> #!/usr/bin/env python
> myString = "Sir, you are an egotistical rhetorician!!!"
> length=len(myString)
> extra=length%4
> if extra > 0:
>   myString = myString+("#"*extra)+"#"
> r = range(0, len(myString), 4)
> wordRange=[]
> for i in r:
>   if i>1:
> wordRange.append((int(i-4),i))
> for t in wordRange:
>   print myString[t[0]:t[1]]
>
> Surely there's an easier way?
>
> S.

How's this?:

>>> myString = "Sir, you are an egotistical rhetorician!!!"
>>> [myString[i:i+4] for i in range(0, len(myString), 4)]
['Sir,', ' you', ' are', ' an ', 'egot', 'isti', 'cal ', 'rhet',
'oric', 'ian!', '!!']

Hopefully that should point you in the right direction to do n-sized
words as well.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Splitting a string into n-sized bytes

2006-03-14 Thread Steve Nelson
Hello all,

Further to my previous puzzling, I've been working out the best way to
chop a string up into n-sized words:

I'm aware that I can use a slice of the string, with, eg, myWord[0:4].

I am also aware that I can do blob = myWord.pop(0) to take (and
remove) the first character.  I can botch these together, eg
myFourLetterWord =
myWord.pop(0)+myWord.pop(0)+myWord.pop(0)+myWord.pop(0) but that is
really horrid.

I then tried doing something suitably silly, as follows:

1) Find the length of string.
2) Create a list using range(0, length, 4)
3) We now have the end points for each 'word', eg 4, 8, 12.
4) Now create a list of tuples that represent the slices we need, ie
(0,4), (5,8) etc
5) Iterate over this list, grabbing the slices as depicted in the tuples.
6) Use these tuples to grab slices.

The code looked like this:
#!/usr/bin/env python
myString = "Sir, you are an egotistical rhetorician!!!"
length=len(myString)
extra=length%4
if extra > 0:
  myString = myString+("#"*extra)+"#"
r = range(0, len(myString), 4)
wordRange=[]
for i in r:
  if i>1:
wordRange.append((int(i-4),i))
for t in wordRange:
  print myString[t[0]:t[1]]

Surely there's an easier way?

S.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor