python - Repeat list if index range is out of bounds -
i have python list
a = [1, 2, 3, 4]
and i'd range of indices such if select indices 0
through n
, i'm getting (for n=10
) repeated
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2]
i of course repeat list via (int(float(n) / len(a) - 0.5) + 1) * a
first , select range [0:10]
out of that, feels rather clumsy.
any hints?
you can use modulo operator when accessing list, i.e.
a[i % len(a)]
this give same result, doesn't require store redundant elements.
Comments
Post a Comment