繁体   English   中英

如何增量增加同一词典中的所有元组

[英]How to incrementally increase all tuples within the same dictionary

我有一本字典,它生成一个“地图”,例如元组(x,y):mapTile

mapGenCode = {(1, 1): 0, (2, 1): 1, (3, 1): 0, (1, 2): 0, (2, 2): 2, (3, 2): 1}

我想要实现的是,当按下键盘上的箭头时,同一词典中的所有x / y值都将增加+ 1 / -1

if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            x_loc = x_loc-1
            x_map -1

mapRefresh(x_map, y_map)

def mapRefresh(x, y):

    if x == -1:
        for (key1, key2), value in mapGenCode.iteritems():
            newkeyX = key1 -1
            mapGenCode[newKey1, key2] = value

我以前遇到过这个错误

runtimeerror: dictionary changed size during iteration

您可以考虑以下代码,而不是“ for key,dic中的值”

while key in dic:
    func()
    key += 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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