而不是这样做:
for x in range(500):
for y in range(300):
print x,y
我该怎么办?
for x,y in range(500),range(300):
print x,y
答案 0 :(得分:3)
我会使用itertools.product
from itertools import product
for x, y in product(range(500), range(300)):
print x, y
答案 1 :(得分:2)
pairs = [ (i,j) for i in range(300) for j in range(300) ]
print pairs
这是我运行的测试:
print [ (i,j) for i in range(3) for j in range(3) ]
输出
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
print [ (i,j) for i, j in itertools.product(range(300), range(300))]