是否有一个不那么冗长的替代方案:
for x in xrange(array.shape[0]):
for y in xrange(array.shape[1]):
do_stuff(x, y)
Run Code Online (Sandbox Code Playgroud)
我想出了这个:
for x, y in itertools.product(map(xrange, array.shape)):
do_stuff(x, y)
Run Code Online (Sandbox Code Playgroud)
这节省了一个缩进,但仍然非常难看.
我希望看起来像这个伪代码的东西:
for x, y in array.indices:
do_stuff(x, y)
Run Code Online (Sandbox Code Playgroud)
这样的事情存在吗?