我有一組隨機執行的操作。其中一個操作會迭代字典。我希望每次調用這個動作時,字典都會迭代到字典的下一個位置,而不是第一個位置。保存字典迭代位置以便以後繼續
如果我只需要一個值,我可以使用列表而不是字典,將索引列表保存在函數調用之外,並將列表的最後一個索引傳遞給函數。但我需要價值,關鍵和價值。我可以使用2個列表,將鍵值存儲在一箇中,將值存儲在另一個值中,將索引保存在函數調用之外,並在每次調用action_two時傳遞最後一個索引,但是可能有更簡單的方法使用字典通過保存字典迭代的位置,我不需要使用2個列表?
import random
import time
def action_one(): print "action 1"
def action_two():
diccionarios_grupos_ids = {
'580864492030176':'Rafaela Argentina',
'314744565339924':'Ventas Rafaelinas',
'976386572414848':'Ventas en Rafaela y Zona',
'157271887802087':'Rafaela Vende',
'77937415209':'Mas Poco Vendo',
'400258686677963':'Clasificados Rafaela',
'1708071822797472':'Vende Susana Roca Bella Italia Lehmann San Antonio Villa San Jose y Rafaela',
'639823676133828':'[email protected] [email protected] de las ofertas sunchales!!!!!!',
'686381434770519':'H&M Computacion',
'1489889931229181':'RAFAELA Compra/Venta',
'228598317265312':'Compra-Venta Rafaela',
'406571412689579':'Alta Venta'}
for key,value in diccionarios_grupos_ids.iteritems():
print key,value
# I want to iterate in the next position the next time action_two is called
break
def action_three(): print "action 3"
lista_acciones = [action_one,action_two,action_three]
while True:
tiempo_random = random.randint(1,3)
time.sleep(tiempo_random)
choice = random.choice(lista_acciones)
choice()
輸出示例會有所幫助。目前還不清楚你在問什麼。 – ergonaut
您以後需要列表中的元素嗎?如果不是,爲什麼不在引用它們之後從列表中刪除它們,而只是繼續引用列表的第一個元素? –