保存字典迭代位置以继续

2024-09-28 21:23:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一套我想随机执行的动作。其中一个操作在字典上迭代。我希望每次调用此操作时,字典都会迭代到字典的下一个位置,而不是第一个位置。你知道吗

如果我只需要一个值,我可以使用列表而不是字典,将索引列表保存在函数调用之外,并将列表的最后一个索引传递给函数。但我需要两个价值观,关键和价值。我可以使用两个列表,将键存储在一个列表中,将值存储在另一个列表中,将索引保存在函数调用之外,并在每次调用action\u 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()

Tags: import列表字典timedefactionrandomone
2条回答

iteritem()赋给变量,例如:

iterable = diccionarios_grupos_ids.iteritems()

for key,value in iterable:
    ...

您需要解决的问题是使用iterable来多次调用action_two()最简单的是全局变量,但全局变量通常被认为是错误的样式:

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 action_two():
    print next(iterable)

可以使用生成器:

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_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

有关更多信息,请查看Understanding Generators in Python

还要注意,dict不是有序的,因此不能保证您的键/值会以任何可复制的顺序进行迭代。如果这对您很重要,那么您应该使用OrderedDict。你知道吗

相关问题 更多 >