如何在此循环中查找迭代索引

时间:2014-02-28 19:26:36

标签: python loops dictionary iterator

我正在寻找一种用迭代号替换'row'的方法。

directions = 'down', 'up', 'left', 'right'
actions = 'idle', 'walk', 'fight', 'death'
frames = range(4)
duration = 0.2

animations = {
    action : {
        direction : (
            (duration, ('row', x)) for x in frames
        ) for direction in directions
    } for action in actions
}

例如,'idle'down down将为0,up 1,left 2,right 3,然后是walk up 4,down 5,依此类推。

2 个答案:

答案 0 :(得分:2)

您可以使用enumerate()来计算迭代次数:

animations = {
    action : {
        direction : (
            (duration, (i * len(directions) + j, x)) for x in frames
        ) for j, direction in enumerate(directions)
    } for i, action in enumerate(actions)
}

这会产生:

>>> pprint({action: {direction: [(duration, (i * len(directions) + j, x)) for x in frames] for j, direction in enumerate(directions) } for i, action in enumerate(actions)})
{'death': {'down': [(0.2, (12, 0)),
                    (0.2, (12, 1)),
                    (0.2, (12, 2)),
                    (0.2, (12, 3))],
           'left': [(0.2, (14, 0)),
                    (0.2, (14, 1)),
                    (0.2, (14, 2)),
                    (0.2, (14, 3))],
           'right': [(0.2, (15, 0)),
                     (0.2, (15, 1)),
                     (0.2, (15, 2)),
                     (0.2, (15, 3))],
           'up': [(0.2, (13, 0)),
                  (0.2, (13, 1)),
                  (0.2, (13, 2)),
                  (0.2, (13, 3))]},
 'fight': {'down': [(0.2, (8, 0)),
                    (0.2, (8, 1)),
                    (0.2, (8, 2)),
                    (0.2, (8, 3))],
           'left': [(0.2, (10, 0)),
                    (0.2, (10, 1)),
                    (0.2, (10, 2)),
                    (0.2, (10, 3))],
           'right': [(0.2, (11, 0)),
                     (0.2, (11, 1)),
                     (0.2, (11, 2)),
                     (0.2, (11, 3))],
           'up': [(0.2, (9, 0)), (0.2, (9, 1)), (0.2, (9, 2)), (0.2, (9, 3))]},
 'idle': {'down': [(0.2, (0, 0)),
                   (0.2, (0, 1)),
                   (0.2, (0, 2)),
                   (0.2, (0, 3))],
          'left': [(0.2, (2, 0)),
                   (0.2, (2, 1)),
                   (0.2, (2, 2)),
                   (0.2, (2, 3))],
          'right': [(0.2, (3, 0)),
                    (0.2, (3, 1)),
                    (0.2, (3, 2)),
                    (0.2, (3, 3))],
          'up': [(0.2, (1, 0)), (0.2, (1, 1)), (0.2, (1, 2)), (0.2, (1, 3))]},
 'walk': {'down': [(0.2, (4, 0)),
                   (0.2, (4, 1)),
                   (0.2, (4, 2)),
                   (0.2, (4, 3))],
          'left': [(0.2, (6, 0)),
                   (0.2, (6, 1)),
                   (0.2, (6, 2)),
                   (0.2, (6, 3))],
          'right': [(0.2, (7, 0)),
                    (0.2, (7, 1)),
                    (0.2, (7, 2)),
                    (0.2, (7, 3))],
          'up': [(0.2, (5, 0)), (0.2, (5, 1)), (0.2, (5, 2)), (0.2, (5, 3))]}}

或者,使用itertools.count() objectnext();如果内部循环使用基于外部循环的可变数量的元素,这将非常有用:

from itertools import count

iter_count = count()

animations = {
    action : {
        direction : (
            (duration, (count, x)) for c in (next(iter_count),) for x in frames
        ) for direction in directions
    } for action in actions
}

答案 1 :(得分:0)

我只是建议制作一个计数器,并使用for循环构建您的结构:

animations = {}
row = 0

for action in actions:
    d = {}
    for direction in directions:
        rows = []
        for x in frames:
            rows.append((duration, (row, x))
            row += 1
        d[direction] = rows
    animations[action] = d