Python numpy: create 2d array of values based on coordinates

前端 未结 5 837
太阳男子
太阳男子 2020-12-24 15:34

I have a file containing 3 columns, where the first two are coordinates (x,y) and the third is a value (z) corresponding to that position. Here\'s a short example:



        
5条回答
  •  鱼传尺愫
    2020-12-24 16:07

    You could try something like:

    import numpy as np
    
    x = [0, 0, 1, 1, 2, 2]
    y = [1, 2, 0, 1, 1, 2]
    z = [14, 17, 15, 16, 18, 13]
    
    arr = np.zeros((3,3))
    yx = zip(y,x)
    
    for i, coord in enumerate(yx):
        arr[coord] = z[i]
    
    print arr
    >>> [[  0.  15.   0.]
         [ 14.  16.  18.]
         [ 17.   0.  13.]]
    

提交回复
热议问题