Is there a multi-dimensional version of arange/linspace in numpy?

前端 未结 9 1489
有刺的猬
有刺的猬 2020-12-02 08:31

I would like a list of 2d NumPy arrays (x,y) , where each x is in {-5, -4.5, -4, -3.5, ..., 3.5, 4, 4.5, 5} and the same for y.

I could do

x = np.ar         


        
9条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 09:16

    Not sure if I understand the question - to make a list of 2-element NumPy arrays, this works:

    import numpy as np
    x = np.arange(-5, 5.1, 0.5)
    X, Y = np.meshgrid(x, x)
    Liszt = [np.array(thing) for thing in zip(X.flatten(), Y.flatten())] # for python 2.7
    

    zip gives you a list of tuples, and the list comprehension does the rest.

提交回复
热议问题