对于多维范围,是否存在Python等效范围(n)? [英] Is there a Python equivalent of range(n) for multidimensional ranges?

查看:110
本文介绍了对于多维范围,是否存在Python等效范围(n)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python上,range(3)将返回[0,1,2]。是否有多维范围的等价物?

On Python, range(3) will return [0,1,2]. Is there an equivalent for multidimensional ranges?

range((3,2)) # [(0,0),(0,1),(1,0),(1,1),(2,0),(2,1)]

因此,例如,在基于图块的游戏中循环矩形区域的图块可以写成:

So, for example, looping though the tiles of a rectangular area on a tile-based game could be written as:

for x,y in range((3,2)):

注意I我不是要求实施。我想知道这是否是一个公认的模式,如果在Python或它的标准/公共库上有内置函数。

Note I'm not asking for an implementation. I would like to know if this is a recognized pattern and if there is a built-in function on Python or it's standard/common libraries.

推荐答案

在numpy中,它是 numpy .ndindex 。另请参阅 numpy.ndenumerate

In numpy, it's numpy.ndindex. Also have a look at numpy.ndenumerate.

例如

import numpy as np
for x, y in np.ndindex((3,2)):
    print x, y

此收益率:

0 0
0 1
1 0
1 1
2 0
2 1

这篇关于对于多维范围,是否存在Python等效范围(n)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆