Splitting a string into substrings of equal size
Mark Tolonen
metolone+gmane at gmail.com
Sat Aug 15 16:22:39 EDT 2009
"Gregor Lingl" <gregor.lingl at aon.at> wrote in message
news:4a87036a$0$2292$91cee783 at 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 ii in range(0,len(jj),3)])
>> '123,456,789,012,345,678,9'
>> >>>
>>
>> Emile
>>
>
> Less beautiful but more correct:
>
> >>> ",".join([jj[max(ii-3,0):ii] for ii in
> range(len(jj)%3,len(jj)+3,3)])
> '1,234,567,890,123,456,789'
>
> Gregor
Is it?
>>> jj = '234567890123456789'
>>> ",".join([jj[max(ii-3,0):ii] for ii in range(len(jj)%3,len(jj)+3,3)])
',234,567,890,123,456,789'
At least one other solution in this thread had the same problem.
-Mark
More information about the Python-list
mailing list