Anonymous Entity
Anonymous Entity

Reputation: 3350

How to find the iteration index in this loop

I'm looking for a way to replace 'row' with the iteration number.

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
}

So for example in 'idle' down will be 0, up 1, left 2, right 3, then in walk up 4, down 5 and so on.

Upvotes: 0

Views: 138

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1124110

You can use enumerate() to calculate your iterations:

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)
}

This produces:

>>> 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))]}}

Alternatively, use a itertools.count() object with next(); this is helpful if your inner loop uses a variable number of elements based on the outer loop:

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
}

Upvotes: 2

nneonneo
nneonneo

Reputation: 179602

I would simply suggest making a counter, and building your structures using for loops:

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

Upvotes: 0

Related Questions