Traversing through variable-sized lists

Andrej Mitrovic andrej.mitrovich at
Wed Feb 17 19:10:37 CET 2010


I couldn't figure out a better description for the Subject line, but
anyway, I have the following:

_num_frames = 32
_frames = range(0, _num_frames) # This is a list of actual objects,
I'm just pseudocoding here.
_values = [0, 1, 2, 3, 4]

I want to call a function of _frames for each frame with a _values
argument, but in a way to "spread out" the actual values.

I would want something similar to the following to be called:


Both the _values list and _frames list can be of variable and uneven
size, which is what is giving me the problems. I'm using Python 2.6.

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]

There has to be a better way to do this. I'd appreciate any help.

More information about the Python-list mailing list