Orbit09
Orbit09

Reputation: 215

How to iterate over a dictionary of tuples

I have a list of tuples called possible_moves containing possible moves on a board in my game:

[(2, 1), (2, 2), (2, 3), (3, 1), (4, 5), (5, 2), (5, 3), (6, 0), (6, 2), (7, 1)]

Then, I have a dictionary that assigns a value to each cell on the game board:

{(0,0): 10000, (0,1): -3000, (0,2): 1000, (0,3): 800, etc.}

I want to iterate over all possible moves and find the move with the highest value.

my_value = 0
possible_moves = dict(possible_moves)
for move, value in moves_values:
    if move in possible_moves and possible_moves[move] > my_value:
        my_move = possible_moves[move]
        my_value = value
return my_move

The problem is in the part for move, value, because it creates two integer indexes, but I want the index move to be a tuple.

Upvotes: 2

Views: 88

Answers (4)

jpp
jpp

Reputation: 164693

You can use max with dict.get:

possible_moves = [(2, 1), (2, 2), (2, 3), (3, 1), (4, 5), (5, 2),
                  (5, 3), (6, 0), (6, 2), (7, 1), (0, 2), (0, 1)]

scores = {(0,0): 10000, (0,1): -3000, (0,2): 1000, (0,3): 800}

res = max(possible_moves, key=lambda x: scores.get(x, 0))  # (0, 2)

This assumes moves not found in your dictionary have a default score of 0. If you can guarantee that every move is included as a key in your scores dictionary, you can simplify somewhat:

res = max(possible_moves, key=scores.__getitem__)

Note the syntax [] is syntactic sugar for __getitem__: if the key isn't found you'll meet KeyError.

Upvotes: 2

timgeb
timgeb

Reputation: 78700

IIUC, you don't even need the list of possible moves. The moves and their scores you care about are already contained in the dictionary.

>>> from operator import itemgetter
>>>
>>> scores = {(0,0): 10000, (0,1): -3000, (0,2): 1000, (0,3): 800}
>>> max_move, max_score = max(scores.items(), key=itemgetter(1))
>>>
>>> max_move
(0, 0)
>>> max_score
10000

edit: turns out I did not understand quite correctly. Assuming that the list of moves, let's call it possible_moves, contains the moves possible right now and that the dictionary scores contains the scores for all moves, even the impossible ones, you can issue:

max_score, max_move = max((scores[move], move) for move in possible_moves)

... or if you don't need the score:

max_move = max(possible_moves, key=scores.get)

Upvotes: 2

wishmaster
wishmaster

Reputation: 1487

possibleMoves=[(2, 1), (2, 2), (2, 3), (3, 1), (4, 5), (5, 2),(0, 3),(5, 3), (6, 0), (6, 2), (7, 1),(0,2)]
movevalues={(0,0): 10000, (0,1): -3000, (0,2): 1000, (0,3): 800}
def func():
    my_value=0
    for i in range(len(possibleMoves)):
        for k,v in movevalues.items():
            if possibleMoves[i]==k and v>my_value:


                my_value=v
    return my_value
maxValue=func()
print(maxValue)

Upvotes: 0

Amadan
Amadan

Reputation: 198334

If d is a dict, iterator of d generates keys. d.items() generates key-value pairs. So:

for move, value in moves_values.items():

Upvotes: 0

Related Questions