Re: [Tutor] Output of list

2007-12-23 Thread János Juhász
Dear Emil,

 I want to be capable of converting a string into a list where all 
 the items, in  the list, have a fixed length not equal to 1 e.g i 
 have k = 'abcdefgh' and I want the fixed length for all the the 
 items to be 2 then the list would look like ['ab', 'cd', 'ef, 'gh'].
 How do i do this?

 
 - Emil

It is nice place to use a generator:

def pairs(sliceit):
streamlist = list(sliceit)
streamlist.reverse()
while streamlist:
pair = streamlist.pop() 
try:pair += streamlist.pop()
except: pass
yield pair

## Probably it is easier to understand
def pairs2(sliceit):
try:
while sliceit:
yield sliceit[:2]
sliceit = sliceit[2:]
except: # oops, it was odd length
yield sliceit


print '_'.join(e for e in pairs('abcd'))
print '_'.join(e for e in pairs('abcde'))
print '_'.join(e for e in pairs2('abcd'))
print '_'.join(e for e in pairs2('abcde'))


Best Regards,
Janos
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Output of list

2007-12-23 Thread Martin Walsh
János Juhász wrote:
  It is nice place to use a generator:
 
 def pairs(sliceit):
 streamlist = list(sliceit)
 streamlist.reverse()
 while streamlist:
 pair = streamlist.pop() 
 try:pair += streamlist.pop()
 except: pass
 yield pair
 
 ## Probably it is easier to understand
 def pairs2(sliceit):
 try:
 while sliceit:
 yield sliceit[:2]
 sliceit = sliceit[2:]
 except: # oops, it was odd length
 yield sliceit
 

... Or, by extending Alan's solution ...

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

k = 'abcdefghi'
list(splitStringByN(k, 2))

As it turns out, this is similar to an ASPN Cookbook recipe contributed
by Dmitry Vasiliev:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302069

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


Re: [Tutor] Output of list

2007-12-23 Thread János Juhász
Dear Marty,


... Or, by extending Alan's solution ...

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

k = 'abcdefghi'
list(splitStringByN(k, 2))

It seems to be the most readable solution for me.


As it turns out, this is similar to an ASPN Cookbook recipe contributed
by Dmitry Vasiliev:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302069

HTH,
Marty

Thanks for the link.


Best regards,
Janos
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Output of list

2007-12-23 Thread Martin Walsh
János Juhász wrote:
 Dear Marty,

Hi Janos,

 ... Or, by extending Alan's solution ...

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

 k = 'abcdefghi'
 list(splitStringByN(k, 2))
 
 It seems to be the most readable solution for me.

For completeness, one could also pull it out of the function def and use
a generator expression directly. If I'm not mistaken, this would be more
or less equivalent:

k = 'abcdefghi'
list((k[m:m+2] for m in range(0, len(k), 2)))

 
 
 As it turns out, this is similar to an ASPN Cookbook recipe contributed
 by Dmitry Vasiliev:
 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302069

 HTH,
 Marty
 
 Thanks for the link.

No problem.

Happy Holidays!
Marty
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Output of list

2007-12-22 Thread Ricardo Aráoz
Emil wrote:
 hey
 
 I want to be capable of converting a string into a list where all the items, 
 in  the list, have a fixed length not equal to 1 e.g i have k = 'abcdefgh' 
 and I want the fixed length for all the the items to be 2 then the list would 
 look like ['ab', 'cd', 'ef, 'gh']. How do i do this?
 
 

map(''.join, zip(k[::2], k[1::2]))
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Output of list

2007-12-22 Thread Ricardo Aráoz
Emil wrote:
 hey
 
 I want to be capable of converting a string into a list where all the items, 
 in  the list, have a fixed length not equal to 1 e.g i have k = 'abcdefgh' 
 and I want the fixed length for all the the items to be 2 then the list would 
 look like ['ab', 'cd', 'ef, 'gh']. How do i do this?
 
 

Also : [''.join(i) for i in zip(k[::2], k[1::2])]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Output of list

2007-12-22 Thread Martin Walsh
Ricardo Aráoz wrote:
 Emil wrote:
 hey

 I want to be capable of converting a string into a list where all the items, 
 in  the list, have a fixed length not equal to 1 e.g i have k = 'abcdefgh' 
 and I want the fixed length for all the the items to be 2 then the list 
 would look like ['ab', 'cd', 'ef, 'gh']. How do i do this?


 
 Also : [''.join(i) for i in zip(k[::2], k[1::2])]

Cool use of 'zip' and extended slicing!

Just thought I would add that 'zip' truncates after the shortest
sequence, which would cause data loss for strings of odd length -- of
course, the OP may not consider this a problem.

In [1]: k = 'abcdefghi' # - note the 'i'

In [2]: len(k)
Out[2]: 9

In [3]: [''.join(i) for i in zip(k[::2], k[1::2])]
Out[3]: ['ab', 'cd', 'ef', 'gh'] # - 'i' is gone


HTH,
Marty





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


Re: [Tutor] Output of list

2007-12-22 Thread Alan Gauld

Emil [EMAIL PROTECTED] wrote

 I want to be capable of converting a string into a list where all 
 the items,
  in  the list, have a fixed length not equal to 1

You can use list slicing to do this.
Combine with the stepsize parameter of the range function

 k = 'abcdefgh' and I want the fixed length for all the the
 items to be 2 then the list would look like ['ab', 'cd', 'ef, 'gh'].

k = 'abcdefgh'
new = []
for n in range(0,len(k),2):  # 2=stepsize
 new.append(k[n:n+2])   # use slice
print new

As a list comprehension that would be:

new = [k[n:n+2] for n in range(0,len(k),2)]

And as a more general function:

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

Should work,

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


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


Re: [Tutor] Output of list

2007-12-22 Thread Ricardo Aráoz
Martin Walsh wrote:
 Ricardo Aráoz wrote:
 Emil wrote:
 hey

 I want to be capable of converting a string into a list where all the 
 items, in  the list, have a fixed length not equal to 1 e.g i have k = 
 'abcdefgh' and I want the fixed length for all the the items to be 2 then 
 the list would look like ['ab', 'cd', 'ef, 'gh']. How do i do this?


 Also : [''.join(i) for i in zip(k[::2], k[1::2])]
 
 Cool use of 'zip' and extended slicing!
 

Thx

 Just thought I would add that 'zip' truncates after the shortest
 sequence, which would cause data loss for strings of odd length -- of
 course, the OP may not consider this a problem.
 
 In [1]: k = 'abcdefghi' # - note the 'i'
 
 In [2]: len(k)
 Out[2]: 9
 
 In [3]: [''.join(i) for i in zip(k[::2], k[1::2])]
 Out[3]: ['ab', 'cd', 'ef', 'gh'] # - 'i' is gone
 

Could only think of :
[''.join(i) for i in zip(k[::2], k[1::2])] + list(k)[-(len(k)%2):0:-len(k)]

But maybe it is too complicated. There should be a simpler way.


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