我有一个python循环,每次调用函数时迭代到下一个货币对(ACTIVES)。有人能帮上忙吗?这个代码可以跳转到循环的开始(在本例中是EUR美元),即使迭代还没有到达循环的末尾,而且还在说(USDJPY)?谢谢。这是我的代码:
# ACTIVES ITERATE FUNCTION
ACTIVES_alternator = cycle(('EURUSD','EURJPY','GBPUSD','USDJPY','GBPJPY','EURGBP'))
def iterate_action():
global ACTIVES
global ACTIVES_Alternator
ACTIVES = next(ACTIVES_alternator)
print("Next actives: ",ACTIVES)
# ACTIVES ITERATE FUNCTION
发布于 2022-10-09 07:38:46
这将通过ACTIVES_alternator循环,但您必须提供起点.
# ACTIVES ITERATE FUNCTION
ACTIVES_alternator = ['EURUSD','EURJPY','GBPUSD','USDJPY','GBPJPY','EURGBP']
def iterate_action(actives_alternator, current_active):
index = actives_alternator.index(current_active)+1
if index > len(actives_alternator)-1:
index = 0
next_active = actives_alternator[index]
print("Next actives: ", next_active)
return next_active
# ACTIVES ITERATE FUNCTION
if __name__ == "__main__":
current_active = "EURUSD"
while True:
current_active = iterate_action(ACTIVES_alternator, current_active)
输出:
Next actives: EURUSD
Next actives: EURJPY
Next actives: GBPUSD
Next actives: USDJPY
Next actives: GBPJPY
Next actives: EURGBP
Next actives: EURUSD
Next actives: EURJPY
Next actives: GBPUSD
Next actives: USDJPY
Next actives: GBPJPY
Next actives: EURGBP
Next actives: EURUSD
Next actives: EURJPY
Next actives: GBPUSD
Next actives: USDJPY
Next actives: GBPJPY
Next actives: EURGBP
编辑:您的代码运行良好:
import itertoolet
# ACTIVES ITERATE FUNCTION
ACTIVES_alternator = itertools.cycle(('EURUSD','EURJPY','GBPUSD','USDJPY','GBPJPY','EURGBP'))
def iterate_action():
global ACTIVES
global ACTIVES_Alternator
ACTIVES = next(ACTIVES_alternator)
print("Next actives: ",ACTIVES)
return ACTIVES
# ACTIVES ITERATE FUNCTION
if __name__ == "__main__":
while True:
current_active = iterate_action()
最后编辑:
from itertools import *
# ACTIVES ITERATE FUNCTION
ACTIVES_alternator = ['EURUSD','EURJPY','GBPUSD','USDJPY','GBPJPY','EURGBP']
ACTIVES_alternator_cycle = cycle(ACTIVES_alternator)
def iterate_action():
ACTIVES = next(ACTIVES_alternator_cycle)
print("Next actives: ",ACTIVES)
return ACTIVES
# ACTIVES ITERATE FUNCTION
def incrementPos(current):
current += 1
if current > (len(ACTIVES_alternator)-1):
current = 0
return current
if __name__ == "__main__":
currentPos = 0
current_active = iterate_action()
currentPos = incrementPos(currentPos)
current_active = iterate_action()
currentPos = incrementPos(currentPos)
current_active = iterate_action()
currentPos = incrementPos(currentPos)
current_active = iterate_action()
ACTIVES_alternator_cycle = islice(ACTIVES_alternator_cycle, (len(ACTIVES_alternator)-1) - currentPos, None)
currentPos = 0
current_active = iterate_action()
currentPos = incrementPos(currentPos)
current_active = iterate_action()
currentPos = incrementPos(currentPos)
current_active = iterate_action()
ACTIVES_alternator_cycle = islice(ACTIVES_alternator_cycle, (len(ACTIVES_alternator)-1) - currentPos, None)
currentPos = 0
current_active = iterate_action()
currentPos = incrementPos(currentPos)
current_active = iterate_action()
ACTIVES_alternator_cycle = islice(ACTIVES_alternator_cycle, (len(ACTIVES_alternator)-1) - currentPos, None)
currentPos = 0
产出:
Next actives: EURUSD
Next actives: EURJPY
Next actives: GBPUSD
Next actives: USDJPY
Next actives: EURUSD
Next actives: EURJPY
Next actives: GBPUSD
Next actives: EURUSD
Next actives: EURJPY
https://stackoverflow.com/questions/74002828
复制相似问题