Sequence-spreading
Alex Martelli
aleaxit at yahoo.com
Tue Mar 20 04:36:01 EST 2001
"Rikard Bosnjakovic" <rikbo716 at student.liu.se> wrote in message
news:3AB7167F.2816AA21 at student.liu.se...
> Hi.
>
> 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?
The following is simpler:
offsets = [ [] for i in range(pm) ]
for i in range(len(arg)):
offsets[i%pm].append(i)
Alex
More information about the Python-list
mailing list