【问题标题】:Saving dictionary iteration position for continuing later保存字典迭代位置以供以后继续
【发布时间】:2017-05-26 13:26:36
【问题描述】:

我有一组想要随机执行的操作。其中一个操作遍历字典。我希望每次调用此操作时,字典都会迭代到字典的下一个位置,而不是第一个位置。

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

【问题讨论】:

  • 一个示例输出会有所帮助。不清楚你在问什么。
  • 以后需要列表的元素吗?如果不是,为什么不在引用它们后将它们从列表中删除并继续引用列表的第一个元素?

标签: python dictionary iteration


【解决方案1】:

您可以使用生成器:

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

【讨论】:

    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-12
      • 2019-03-28
      • 2021-01-15
      • 1970-01-01
      • 2017-07-26
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多