首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >函数跳转到迭代工具迭代周期的起始值。

函数跳转到迭代工具迭代周期的起始值。
EN

Stack Overflow用户
提问于 2022-10-09 07:23:29
回答 1查看 35关注 0票数 0

我有一个python循环,每次调用函数时迭代到下一个货币对(ACTIVES)。有人能帮上忙吗?这个代码可以跳转到循环的开始(在本例中是EUR美元),即使迭代还没有到达循环的末尾,而且还在说(USDJPY)?谢谢。这是我的代码:

代码语言:javascript
运行
复制
# 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
EN

回答 1

Stack Overflow用户

发布于 2022-10-09 07:38:46

这将通过ACTIVES_alternator循环,但您必须提供起点.

代码语言:javascript
运行
复制
# 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)

输出:

代码语言:javascript
运行
复制
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

编辑:您的代码运行良好:

代码语言:javascript
运行
复制
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()

最后编辑:

代码语言:javascript
运行
复制
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 

产出:

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74002828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档