Split a string by length

Yermat loic at fejoz.net
Thu Mar 25 15:29:19 CET 2004


Peter Otten a écrit :
> Yermat wrote:
> 
> 
>>take care with that kind of comparisons...
> 
> 
> I'll add a disclaimer next time :-)
> 
> 
>>Especially look at the last two comparison ! the only difference is the
>>construction of the list...
>>
>>so what ? "Beautiful is better than ugly"
>>Make your choice ;-)
> 
> 
> I should have made explicit my "result" that timing doesn't matter much (I
> think) in this case.

I think too. That should only be implementation detail...

>>[..]
>>timeit.py -s"from chunk import chunksiter"
>>"list(chunksiter('aabbccddee', 2))"
>>100000 loops, best of 3: 11.8 usec per loop
>>
>>timeit.py -s"from chunk import chunksiter" "[ x for x in
>>chunksiter('aabbccddee', 2)]"
>>100000 loops, best of 3: 15.2 usec per loop
> 
> 
> These last two timings are useless. The following (and two nested loops
> without continue in either loop or break in the inner loop) is the only
> sane use case:
> 
> 
>>>>[list(ch) for ch in chunks.chunksiter("aabbccdd", 2)]
> 
> [['a', 'a'], ['b', 'b'], ['c', 'c'], ['d', 'd']]
> 
> 
> Looks reasonable. Now let's separate the above in two steps:
> 
> 
>>>>lst = [ch for ch in chunks.chunksiter("aabbccdd", 2)]
>>>>map(list, lst)
> 
> [['a'], ['a'], ['b'], ['b'], ['c'], ['c'], ['d'], ['d']]
> 
> 
> Did you expect that? Errorprone indeed. Of course you could use tee() from
> the itertools example page to make it more robust, but I would expect even
> more overhead...

Well done ! I missed that point !
Make it simple... Finally the chunklist or chunks are my favorite for 
general chunk function. For string I would have use the regexp anyway...

Who want to make a web site with python challenge like that one ? ;-)

yermat




More information about the Python-list mailing list