从numpy数组中提取数据

时间:2014-10-25 16:09:34

标签: python arrays python-2.7 numpy

我有两个numpy数组数据

   grid_code=[1,5,8,7,8,3,40,20....]
   data= Gridcode    X     Y    LINKCODE
            1        ..    ..    0
            1        ..    ..    0
            8        ..    ..    100
            8        ..    ..    100
            10       ..    ..    200
            10       ..    ..    200
            8        ..    ..    111
            8        ..    ..    111

我已经编写了这样的代码

  for i in grid_code:
     new_list=numpy.where(data[:,0]==i)[0]
     mask_list=data[new_list,:]

我想在单独的文件中输出如下:

    Grid code    x    Y
       1        ..   ..
       1        ..   ..

    Grid code     X      Y
       5          ..     ..
       5          ..     ..

    Grid Code     X      Y        ** This is for the one unique link code (eg.for this time 100)
       8          ..     ..
       8          ..     ..

    Grid Code      X      Y        
       7          ..     ..
       7          ..     ..

    Grid Code      X      Y        ** this is for other link code ( this time 111)
       8          ..     ..
       8          ..     ..

我需要根据网格代码提取数据。重复网格代码时遇到问题。当重复网格代码时,我需要根据数组中的第一个LINKCODE进行提取。

我现在尝试使用此示例数据

grid_code=np.array([6,1,9,6,1,6])

data=np.array([[1,50,40,100],
[1,40,20,100],
[6,50,40,5],
[6,50,20,5],
[9,60,90,10],
[9,90,00,10],
[6,100,100,101],
[6,50,90,101],
[6,101,10,101],
[1,11,11,11],
[1,10,10,11],
[6,200,200,102],
[6,200,200,102]])

new=[]
unique=[]
for i in grid_code:
  new_list=numpy.where(data[:,0]==i)[0]
  mask_list=data[new_list,:]
  unique_mask=numpy.unique(mask_list[:,3]).tolist()
  if len(unique_mask)>1:
      unique.append([i])
      new_unique=np.array(unique)
      nq=new_unique[np.where(new_unique[:,0]==i)[0],:]
      if len(nq)>=1:
          b=len(nq)
          a=b-1
          for j in range(a,b):
              p_list=np.where(mask_list[:,3]==unique_mask[j])[0]
              n_list=mask_list[p_list,:]
              print n_list

  else:
      print mask_list

请查看此代码,并建议是否有任何有效的方法来获得相同的输出。

1 个答案:

答案 0 :(得分:1)

如果您在快速,干净且可能不夸张的高效解决方案之后, 这是我的建议

# ev is for everything, let's start with an empty dict
ev = {}

# we identify and label all our stuff
for row in data:

    # unpack a row of the data matrix
    idx1, x, y, idx2 = row

    # the new value for key=idx1 is {} (empty dict) if first time we see
    # idx1, otherwise we keep the old value
    ev[idx1] = ev.get(idx1,{})

    # the new value for key=idx2 of the dictionary that is the value
    # for key=idx1 is the old/default value (a list/the null list)
    # plus a tuple (x,y)
    ev[idx1][idx2] =  ev[idx1].get(idx2,[]).append((x,y))

# we output our carefully labeled collection of x,y data
for idx1 in keys(ev):
    for idx2, xys in ev[idx1]:

        # the choice of the filename is subjective, isn;t it?
        f = open("file%d_%d.out" % (idx1,idx2), "w")

        for x, y in xys:
            f.write("%d %g %g" % (idx1, x, y))

        f.close()