What's the 'correct' python way of doing something to the first element, but something else to the second, and something different again for the third in a list, then repeat, eg:
a = [2, "foo1", "bar1", 5, "foo3", "bar2", 3, "foo2", "bar3"]
my_function(a)
should give
20
foo1
foobar1
50
foo3
foobar2
30
foo2
foobar3
where "my_function" would be something like:
def my_function(a):
i = 0
for line in a:
if i == 0:
line = line*10
if i == 2:
line = "foo"+line
i = i + 1
if i == 3:
i = 0
print line
But this looks very 'unpython'. Is there a better way of doing this, without an int
to keep track? A way of saying, the first time I call this function, do this but the second time, do this, then the third time, do this, then go back to the beginning and do what you did first. A function that keeps track of how many times it has been called.