Re: Splitting a string into substrings of equal size

2009-08-17 Thread Gregor Lingl
Simon Forman schrieb: On Aug 14, 8:22 pm, candide wrote: Suppose you need to split a string into substrings of a given size (except possibly the last substring). I make the hypothesis the first slice is at the end of the string. A typical example is provided by formatting a decimal string with

Re: Splitting a string into substrings of equal size

2009-08-16 Thread Simon Forman
On Aug 14, 8:22 pm, candide wrote: > Suppose you need to split a string into substrings of a given size (except > possibly the last substring). I make the hypothesis the first slice is at the > end of the string. > A typical example is provided by formatting a decimal string with thousands > separ

Re: Splitting a string into substrings of equal size

2009-08-16 Thread Gregor Lingl
Mark Tolonen schrieb: "Gregor Lingl" wrote in message news:4a87036a$0$2292$91cee...@newsreader02.highway.telekom.at... Emile van Sebille schrieb: On 8/14/2009 5:22 PM candide said... ... What is the pythonic way to do this ? I like list comps... >>> jj = '1234567890123456789' >>> ",".

Re: Splitting a string into substrings of equal size

2009-08-15 Thread ryles
On Aug 15, 6:28 pm, MRAB wrote: > >      >>> for z in ["75096042068045", "509", "12024", "7", "2009"]: > >            print re.sub(r"(?<=.)(?=(?:...)+$)", ",", z) > > >     75,096,042,068,045 > >     509 > >     12,024 > >     7 > >     2,009 > > The call replaces a zero-width match with a comma,

Re: Splitting a string into substrings of equal size

2009-08-15 Thread MRAB
Brian wrote: On Sat, Aug 15, 2009 at 4:06 PM, MRAB > wrote: ryles wrote: On Aug 14, 8:22 pm, candide wrote: Suppose you need to split a string into substrings of a given size (except possibly the last substr

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Brian
On Sat, Aug 15, 2009 at 4:06 PM, MRAB wrote: > ryles wrote: > >> On Aug 14, 8:22 pm, candide wrote: >> >>> Suppose you need to split a string into substrings of a given size >>> (except >>> possibly the last substring). I make the hypothesis the first slice is at >>> the >>> end of the string. >

Re: Splitting a string into substrings of equal size

2009-08-15 Thread MRAB
ryles wrote: On Aug 14, 8:22 pm, candide wrote: Suppose you need to split a string into substrings of a given size (except possibly the last substring). I make the hypothesis the first slice is at the end of the string. A typical example is provided by formatting a decimal string with thousands

Re: Splitting a string into substrings of equal size

2009-08-15 Thread ryles
On Aug 14, 8:22 pm, candide wrote: > Suppose you need to split a string into substrings of a given size (except > possibly the last substring). I make the hypothesis the first slice is at the > end of the string. > A typical example is provided by formatting a decimal string with thousands > separ

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Mark Tolonen
"Gregor Lingl" wrote in message news:4a87036a$0$2292$91cee...@newsreader02.highway.telekom.at... Emile van Sebille schrieb: On 8/14/2009 5:22 PM candide said... ... What is the pythonic way to do this ? I like list comps... >>> jj = '1234567890123456789' >>> ",".join([jj[ii:ii+3] for i

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Gregor Lingl
Emile van Sebille schrieb: On 8/14/2009 5:22 PM candide said... ... What is the pythonic way to do this ? I like list comps... >>> jj = '1234567890123456789' >>> ",".join([jj[ii:ii+3] for ii in range(0,len(jj),3)]) '123,456,789,012,345,678,9' >>> Emile Less beautiful but more correct:

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Gregor Lingl
What is the pythonic way to do this ? For my part, i reach to this rather complicated code: # -- def comaSep(z,k=3, sep=','): z=z[::-1] x=[z[k*i:k*(i+1)][::-1] for i in range(1+(len(z)-1)/k)][::-1] return sep.join(x) # Test for z in ["75096042068045", "509",

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Gregor Lingl
What is the pythonic way to do this ? For my part, i reach to this rather complicated code: # -- def comaSep(z,k=3, sep=','): z=z[::-1] x=[z[k*i:k*(i+1)][::-1] for i in range(1+(len(z)-1)/k)][::-1] return sep.join(x) # Test for z in ["75096042068045", "509",

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Emile van Sebille
On 8/14/2009 5:22 PM candide said... Suppose you need to split a string into substrings of a given size (except possibly the last substring). I make the hypothesis the first slice is at the end of the string. A typical example is provided by formatting a decimal string with thousands separator.

Re: Splitting a string into substrings of equal size

2009-08-15 Thread Jan Kaliszewski
Dnia 15-08-2009 o 08:08:14 Rascal wrote: I'm bored for posting this, but here it is: def add_commas(str): str_list = list(str) str_len = len(str) for i in range(3, str_len, 3): str_list.insert(str_len - i, ',') return ''.join(str_list) For short strings (for sure most

Re: Splitting a string into substrings of equal size

2009-08-15 Thread candide
Thanks to all for your response. I particularly appreciate Rascal's solution. -- http://mail.python.org/mailman/listinfo/python-list

Re: Splitting a string into substrings of equal size

2009-08-14 Thread Rascal
I'm bored for posting this, but here it is: def add_commas(str): str_list = list(str) str_len = len(str) for i in range(3, str_len, 3): str_list.insert(str_len - i, ',') return ''.join(str_list) -- http://mail.python.org/mailman/listinfo/python-list

Re: Splitting a string into substrings of equal size

2009-08-14 Thread Jan Kaliszewski
15-08-2009 Jan Kaliszewski wrote: 15-08-2009 candide wrote: Suppose you need to split a string into substrings of a given size (except possibly the last substring). I make the hypothesis the first slice is at the end of the string. A typical example is provided by formatting a decimal str

Re: Splitting a string into substrings of equal size

2009-08-14 Thread Jan Kaliszewski
15-08-2009 candide wrote: Suppose you need to split a string into substrings of a given size (except possibly the last substring). I make the hypothesis the first slice is at the end of the string. A typical example is provided by formatting a decimal string with thousands separator. I'd

Re: Splitting a string into substrings of equal size

2009-08-14 Thread Gabriel Genellina
En Fri, 14 Aug 2009 21:22:57 -0300, candide escribió: Suppose you need to split a string into substrings of a given size (except possibly the last substring). I make the hypothesis the first slice is at the end of the string. A typical example is provided by formatting a decimal string wi

Splitting a string into substrings of equal size

2009-08-14 Thread candide
Suppose you need to split a string into substrings of a given size (except possibly the last substring). I make the hypothesis the first slice is at the end of the string. A typical example is provided by formatting a decimal string with thousands separator. What is the pythonic way to do this ?