numpy创建所有坐标的数组

时间:2018-05-29 08:55:17

标签: python arrays numpy

我想创建一个包含所有可能(x,y)值的列表。

例如 - 对于输入(x = 2,y = 3),输出应为:

np.array([
[0,0],
[0,1],
[0,2],
[1,0],
[1,1],
[1,2]])

订单并不重要

2 个答案:

答案 0 :(得分:5)

又快又脏:

>>> np.argwhere(np.ones((2, 3)))
array([[0, 0],
       [0, 1],
       [0, 2],
       [1, 0],
       [1, 1],
       [1, 2]])

答案 1 :(得分:4)

受到Divakar pd.pivot_table的启发,这应该

$ npm adduser
npm ERR! code E301
npm ERR! Registry returned 301 for POST on 
http://registry.npmjs.org/-/v1/login
...
$ npm config get registry
http://registry.npmjs.org/
$ npm config set registry https://registry.npmjs.org/
$ npm adduser
Username: 
...

def indices_for(m, n):
    i, j = np.ogrid[:m, :n]
    v = np.empty((m, n, 2), dtype=np.uint32)
    v[..., 0] = i
    v[..., 1] = j
    v.shape = (-1, 2)

    return v
>>> indices_for(3, 2)
array([[0, 0],
       [0, 1],
       [0, 2],
       [1, 0],
       [1, 1],
       [1, 2]])