Sequence-spreading

Remco Gerlich scarblac at pino.selwerd.nl
Tue Mar 20 04:00:58 EST 2001


Rikard Bosnjakovic <rikbo716 at student.liu.se> wrote in comp.lang.python:
> I made a small spreading-sequence with the following code:
> 
> ------
> arg = "some string"
> pm = 3
> offsets = []
> for i in xrange(pm):
>     everyX = filter(lambda y: not divmod(y, pm)[1], range(len(arg)))
>     added = map(lambda x: x+i, everyX)
>     offsets.append(filter(lambda x: x<len(arg), added))
> 
> print offsets
> ------
> 
> which gives the correct result:
> 
> >>> ## working on region in file /usr/tmp/python-3115DCp...
> [[0, 3, 6, 9], [1, 4, 7, 10], [2, 5, 8]]
> 
> "pm" is the number of sequences, and then the sequences are increasing.
> 0 in first, 1 in second, 2 in third, 3 in first and so on, until the
> length of the string 'arg' is reached.  However, the loop-code is pretty
> unreadable and awkward.
> 
> Anyone got a better solution for it?

arg = "some string"
pm = 3
offsets = [ [] for i in range(pm) ]
for i in range(len(arg)):
   offsets[i % pm].append(i)


-- 
Remco Gerlich



More information about the Python-list mailing list