Traversing through variable-sized lists
Matt McCredie
mccredie at gmail.com
Wed Feb 17 13:55:32 EST 2010
> I've tried the following workaround, but it often gives me inaccurate
> results (due to integer division), so I had to add a safety check:
>
> num_frames = 32
> values = [0, 1, 2, 3, 4]
> offset_step = num_frames / len(values)
> for index in xrange(0, num_frames):
> offset = index / offset_step
> if offset > offset_values[-1]:
> offset = offset_values[-1]
> frames[index].func(values[offset])
>
> There has to be a better way to do this. I'd appreciate any help.
> Cheers!
>
This is how I would do it, assuming you just want to call the remaining frames
with the last value.
from itertools import izip
def stretch(seq, n):
for val in seq:
for i in xrange(n):
yield val
while True:
yield val
frames_per_value = num_frames // len(values)
for frame, value in izip(frames, stretch(values, frames_per_value)):
frame.func(value)
Matt
More information about the Python-list
mailing list