вівторок, 15 липня 2008 р.

Let's say you have a task: "divide string that consists of 12 chars into string of pairs delimited by ':' char". Well, a good way you can find by yourself, but here is The Evil Way:

>>> from itertools import izip
>>> splitjoin = lambda s, delimiter, size: delimiter.join(''.join(x) for x in (lambda seq, length: izip(*(iter(seq), ) * length))(s, size))
>>> splitjoin("abcdefghijkl", ':', 2)
'ab:cd:ef:gh:ij:kl'

Now I should find a place in the code to put this snippet in. :)

Немає коментарів: