我知道這是一個簡單的問題,但我需要你的幫助。我需要制作一個簡短的代碼,輸出是這樣的:
0 460, 1 3600, 2 2486, 3 460 ,4 3600, 5 2486, 6 460 ....
我寫了這段代碼,但它缺少一些東西......我需要這 237 次才能將這些值提供給變數
a = 0
for i in range(238):
a = 460
print(i, a)
a = 3600
print(i, a)
a = 2486
print(i, a)
因為輸出是這樣的:

uj5u.com熱心網友回復:
將值放在串列中并用于%根據i索引獲取值
vals = [460, 3600, 2486]
for i in range(238):
print(i, vals[i % len(vals)])
輸出
0 460
1 3600
2 2486
3 460
4 3600
5 2486
....
uj5u.com熱心網友回復:
itertools標準庫中的模塊可以在這里提供幫助。你可以寫:
import itertools
g = itertools.cycle((4, 6, 8))
for i in range(238):
print(i, next(g))
uj5u.com熱心網友回復:
從不起眼的單線部門:
print(*[f'{i} {x},' for i,x in zip(range(238), itertools.cycle([460,3600,2486]))])
輸出
0 460, 1 3600, 2 2486, 3 460, 4 3600, 5 2486, 6 460,
(好的,你也需要匯入 itertools)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/514025.html
標籤:Python
