迭代字典作为键的元组

时间:2016-05-26 18:26:53

标签: python json dictionary key tuples

鉴于下面的字典,我需要将列表转换为另一个字典的键。

{'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}]
            ]}

我需要查看字典中是否已存在一组给定的列表但似乎无法迭代它。

1 个答案:

答案 0 :(得分:-1)

如果我是对的,你正在寻找字典的itervalues()和iterkeys()方法。

示例:

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

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

编辑:如果要查找元组,请使用type()函数。例如:

if type(key) is tuple:
    # do stuff