Split a string by length

David McNab david at rebirthing.co.nz
Thu Mar 25 06:14:17 EST 2004


Yermat wrote:
>>> How can I do to simulate this way:
>>> 'aabbcc'.split(2) -> ['aa', 'bb', 'cc']
>>> I tried with a 'slice' but this didn't run:
>>> [item for item in 'aabbcc'[::2]] -> ['a', 'b', 'c']
>> [...]
>  >>> import re
>  >>> re.findall(".{2}","aabbccdd")
> ['aa', 'bb', 'cc', 'dd']
>  >>> re.findall(".{2}","aabbccdde")
> ['aa', 'bb', 'cc', 'dd']

Now, who's gonna benchmark these n different approaches?




More information about the Python-list mailing list