我有一個循環:循環中可能有不穩定的步驟嗎?
result = []
list = ['a', 'b', 'c', 'd', 'e', 'f']
start = 2
step = 5
end = start + step*len(list)
for i in range(start, end, step):
result.append(list[i%len(list)])
print result
在這種情況下,結果將是:
['c', 'b', 'a', 'f', 'e', 'd']
但是,讓我們說,我想要的結果是(起始索引更改爲1):
['b', 'a', 'd', 'c', 'f', 'e']
我如何在每個循環之後進行階躍變更,以便在第一個循環中步驟是5,在下一個循環中它是3,然後是5,等等。
...與另一個循環? – jonrsharpe
so ...你想生成數字序列:'3','8','11','16',...? – mgilson
將你的代碼放入一個函數中,並將該步驟作爲參數傳遞? – dsh