How can I, in python, iterate over multiple 2d lists at once, cleanly?

后端 未结 10 1028
予麋鹿
予麋鹿 2021-02-05 05:15

If I\'m making a simple grid based game, for example, I might have a few 2d lists. One might be for terrain, another might be for objects, etc. Unfortunately, when I need to ite

10条回答
  •  隐瞒了意图╮
    2021-02-05 05:54

    As a slight style change, you could use enumerate:

    for i, arow in enumerate(alist):
        for j, aval in enumerate(arow):
            if aval.isWhatever():
                blist[i][j].doSomething()
    

    I don't think you'll get anything significantly simpler unless you rearrange your data structures as Federico suggests. So that you could turn the last line into something like "aval.b.doSomething()".

提交回复
热议问题