簡體   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