Alternative to nesting for loops in Python

后端 未结 3 1762
小蘑菇
小蘑菇 2020-12-18 10:22

I\'ve read that one of the key beliefs of Python is that flat > nested. However, if I have several variables counting up, what is the alternative to multiple for loops? My c

3条回答
  •  悲&欢浪女
    2020-12-18 10:50

    from itertools import product
    
    def horizontal():
        for x, y in product(range(20), range(17)):
            print 1 + sum(int(n) for n in grid[x][y: y + 4])
    

    You should be using the sum function. Of course you can't if you shadow it with a variable, so I changed it to my_sum

提交回复
热议问题