Python 保存字典迭代位置以便以后继续
我有一系列随机执行的动作。其中一个操作在字典上迭代。我希望每次调用此操作时,字典都会迭代到字典的下一个位置,而不是第一个位置 如果我只需要一个值,我可以使用列表而不是字典,将索引列表保存在函数调用之外,并将列表的最后一个索引传递给函数。但我需要两种价值观,关键和价值。我可以使用两个列表,将键存储在一个列表中,将值存储在另一个列表中,将索引保存在函数调用之外,并在每次调用action_two时传递最后一个索引,但是可能有一种较短的方法来使用字典,即保存字典以某种方式迭代的位置,而我不需要使用两个列表Python 保存字典迭代位置以便以后继续,python,dictionary,iteration,Python,Dictionary,Iteration,我有一系列随机执行的动作。其中一个操作在字典上迭代。我希望每次调用此操作时,字典都会迭代到字典的下一个位置,而不是第一个位置 如果我只需要一个值,我可以使用列表而不是字典,将索引列表保存在函数调用之外,并将列表的最后一个索引传递给函数。但我需要两种价值观,关键和价值。我可以使用两个列表,将键存储在一个列表中,将值存储在另一个列表中,将索引保存在函数调用之外,并在每次调用action_two时传递最后一个索引,但是可能有一种较短的方法来使用字典,即保存字典以某种方式迭代的位置,而我不需要使用两个列
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':'L@s Loc@s 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()
为变量赋值iteritem()
,例如:
iterable = diccionarios_grupos_ids.iteritems()
for key,value in iterable:
...
您需要解决的问题是使用<代码>迭代/<代码>多次调用<代码> ActudioT2()/Case>最简单的是全局变量,但全局变量通常考虑坏的样式:
def action_two():
for key,value in iterable:
print key,value
break
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':'L@s Loc@s de las ofertas sunchales!!!!!!',
'686381434770519':'H&M Computacion',
'1489889931229181':'RAFAELA Compra/Venta',
'228598317265312':'Compra-Venta Rafaela',
'406571412689579':'Alta Venta'}
iterable = iter(diccionarios_grupos_ids.items())
但是如果您只想要下一个,您可以使用next()
:
为变量赋值iteritem()
,例如:
iterable = diccionarios_grupos_ids.iteritems()
for key,value in iterable:
...
您需要解决的问题是使用<代码>迭代/<代码>多次调用<代码> ActudioT2()/Case>最简单的是全局变量,但全局变量通常考虑坏的样式:
def action_two():
for key,value in iterable:
print key,value
break
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':'L@s Loc@s de las ofertas sunchales!!!!!!',
'686381434770519':'H&M Computacion',
'1489889931229181':'RAFAELA Compra/Venta',
'228598317265312':'Compra-Venta Rafaela',
'406571412689579':'Alta Venta'}
iterable = iter(diccionarios_grupos_ids.items())
但是如果您只想要下一个,您可以使用next()
:
您可以使用生成器:
def create_grupos_gen():
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':'L@s Loc@s de las ofertas sunchales!!!!!!',
'686381434770519':'H&M Computacion',
'1489889931229181':'RAFAELA Compra/Venta',
'228598317265312':'Compra-Venta Rafaela',
'406571412689579':'Alta Venta'}
for key,value in cycle(diccionarios_grupos_ids.iteritems()):
yield key, value
grupos_gen = create_grupos_gen()
def action_two():
key, value = next(grupos_gen)
print(key,value)
action_two()
action_two()
# 314744565339924 Ventas Rafaelinas
# 1489889931229181 RAFAELA Compra/Venta
每次调用next(grupos\u gen)
都将提供一个后续的键/值对
如果要循环浏览词典条目,可以使用itertools.cycle
:
from itertools import cycle
def grupos_gen():
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':'L@s Loc@s de las ofertas sunchales!!!!!!',
'686381434770519':'H&M Computacion',
'1489889931229181':'RAFAELA Compra/Venta',
'228598317265312':'Compra-Venta Rafaela',
'406571412689579':'Alta Venta'}
for key,value in cycle(diccionarios_grupos_ids.iteritems()):
yield key, value
grupos_iter = grupos_gen()
def action_two():
key, value = next(grupos_iter)
print(key,value)
for i in range(15):
action_two()
# ('400258686677963', 'Clasificados Rafaela')
# ...
# ('228598317265312', 'Compra-Venta Rafaela')
# ('314744565339924', 'Ventas Rafaelinas')
# ('400258686677963', 'Clasificados Rafaela')
# ('639823676133828', 'L@s Loc@s de las ofertas sunchales!!!!!!')
# ('1708071822797472', 'Vende Susana Roca Bella Italia Lehmann San Antonio Villa
有关更多信息,请查看例如
还要注意,
dict
没有顺序,因此不能保证您的键/值将以任何可复制的顺序进行迭代。如果这对您很重要,您应该使用发电机。您可以使用发电机:
def create_grupos_gen():
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':'L@s Loc@s de las ofertas sunchales!!!!!!',
'686381434770519':'H&M Computacion',
'1489889931229181':'RAFAELA Compra/Venta',
'228598317265312':'Compra-Venta Rafaela',
'406571412689579':'Alta Venta'}
for key,value in cycle(diccionarios_grupos_ids.iteritems()):
yield key, value
grupos_gen = create_grupos_gen()
def action_two():
key, value = next(grupos_gen)
print(key,value)
action_two()
action_two()
# 314744565339924 Ventas Rafaelinas
# 1489889931229181 RAFAELA Compra/Venta
每次调用next(grupos\u gen)
都将提供一个后续的键/值对
如果要循环浏览词典条目,可以使用itertools.cycle
:
from itertools import cycle
def grupos_gen():
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':'L@s Loc@s de las ofertas sunchales!!!!!!',
'686381434770519':'H&M Computacion',
'1489889931229181':'RAFAELA Compra/Venta',
'228598317265312':'Compra-Venta Rafaela',
'406571412689579':'Alta Venta'}
for key,value in cycle(diccionarios_grupos_ids.iteritems()):
yield key, value
grupos_iter = grupos_gen()
def action_two():
key, value = next(grupos_iter)
print(key,value)
for i in range(15):
action_two()
# ('400258686677963', 'Clasificados Rafaela')
# ...
# ('228598317265312', 'Compra-Venta Rafaela')
# ('314744565339924', 'Ventas Rafaelinas')
# ('400258686677963', 'Clasificados Rafaela')
# ('639823676133828', 'L@s Loc@s de las ofertas sunchales!!!!!!')
# ('1708071822797472', 'Vende Susana Roca Bella Italia Lehmann San Antonio Villa
有关更多信息,请查看例如
还要注意,
dict
没有顺序,因此不能保证您的键/值将以任何可复制的顺序进行迭代。如果这对您很重要,您应该使用。示例输出会有所帮助。不清楚你在问什么。你以后需要列表中的元素吗?如果不是,为什么不在引用它们之后将它们从列表中删除,并继续引用列表的第一个元素呢?一个示例输出会有所帮助。不清楚你在问什么。你以后需要列表中的元素吗?如果不是,为什么不在引用它们之后将它们从列表中删除,并继续引用列表的第一个元素?