How can I get rid of multiple nested for loops?

前端 未结 2 1086
你的背包
你的背包 2021-01-25 00:21

I have a Python (3.2) script that searches for points with a property that I want. But it has this ugly part:

for x in range(0,p):
  for y in range(0,p):
    for         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-25 01:13

    You can use something along the lines of:

    for x, y, z in product(range(0,p), range(0,p), range(0,p)):
        print(x,y,z)
    

    or

    for x, y, z in product(range(0,p), repeat=3):
        print(x,y,z)
    

    For python2.7 you need to from itertools import product.

提交回复
热议问题