Splitting a string into substrings of equal size
Gregor Lingl
gregor.lingl at aon.at
Sun Aug 16 05:46:19 EDT 2009
Mark Tolonen schrieb:
>
> "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'
Schluck!
Even more ugly:
",".join([jj[max(ii-3,0):ii] for ii in
range(len(jj)%3,len(jj)+3,3)]).strip(",")
'234,567,890,123,456,789'
Gregor
>
> At least one other solution in this thread had the same problem.
>
> -Mark
>
>
More information about the Python-list
mailing list