简体   繁体   中英

Iterating over a dictionary for tuples as keys

Given a dictionary like the one below there are lists which I need to convert to keys for another dictionary.

{'timer': 100,
 'blocks': [[[5, 1], {'image': 'red.gif', 'points': 10, 'delete': True}],
            [[5, 2], {'image': 'red.gif', 'points': 10, 'bonus': 50, 'delete': True}],
            [[5, 3], {'image': 'red.gif', 'points': 10, 'delete': True}],
            [[5, 4], {'paddle_delta': 1, 'image': 'red.gif', 'points': 10, 'delete': True}],
            [[5, 5], {'image': 'red.gif', 'points': 10, 'delete': True}],
            [[5, 6], {'image': 'red.gif', 'points': 10, 'delete': True}],
            [[5, 7], {'paddle_delta': -1, 'image': 'red.gif', 'points': 10, 'delete': True}],
            [[5, 8], {'image': 'red.gif', 'points': 10, 'delete': True}]
            ]}

I need to see if a given set of lists already exist in the dictionary but can't seem to iterate through it.

If I am right, you are looking for itervalues() and iterkeys() methods of dictionary.

Examples:

for value in dictionary.itervalues():
      # do stuff with value

for key in dictionary.iterkeys():
      # do stuff with key

Edit: if you want to find tuples, use type() function. Example:

if type(key) is tuple:
    # do stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM