Is there a Python equivalent of range(n) for multidimensional ranges?(对于多维范围,是否有 Python 的 range(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)):
请注意,我不是要求实现.我想知道这是否是一种公认的模式,以及 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 的 range(n) 等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:对于多维范围,是否有 Python 的 range(n) 等价物?


基础教程推荐
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 包装空间模型 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01