将numpy数组连接到字典中的现有键

时间:2017-04-12 13:59:30

标签: python arrays numpy dictionary

我有以下问题。我想将numpy数组连接到字典中的键。但我总是得到错误:“所有输入数组必须具有相同数量的维度”。到目前为止我的代码:

def assignCoordinates_to_Cells(self,r):
        tracks={}
        allCoo=self.Coordinates
        for i in range(len(allCoo)):
            trackNumber=int(allCoo[i][4]-1000000000)
            track = np.concatenate((allCoo[i][0:3],tracks.get(trackNumber)),axis=0)
            tracks[trackNumber] = track

self.Coordinates就是这样:

array([[  2.43130000e+01,   2.94679000e+02,   1.50000000e+00,
      1.00000000e+00,   1.00000000e+09],
   [  2.55100000e+01,   2.95263000e+02,   1.50000000e+00,
      2.00000000e+00,   1.00000000e+09],
   [  2.67430000e+01,   2.94526000e+02,   1.50000000e+00,
      3.00000000e+00,   1.00000000e+09],
   ..., 
   [  2.82311000e+02,   5.35420000e+01,   1.50000000e+00,
      1.00000000e+02,   1.00000017e+09],
   [  2.86946000e+02,   5.49790000e+01,   9.17700000e+00,
      1.01000000e+02,   1.00000017e+09],
   [  2.93990000e+02,   5.19340000e+01,   1.29780000e+01,
      1.02000000e+02,   1.00000017e+09]])

我想要这样的东西,但是在numpy数组形状中:

  {...294: [[202.74600000000001, 103.483, 1.5],
  [202.91200000000001, 103.79600000000001, 6.4909999999999997],
  [200.54900000000001, 103.48699999999999, 7.4619999999999997],
  [193.04300000000001, 104.059, 10.295999999999999],
  [189.28200000000001, 103.102, 13.153],
  [190.76300000000001, 104.33499999999999, 14.630000000000001]],
 295: [[181.733, 86.781999999999996, 26.329999999999998],
  [182.86600000000001, 85.310000000000002, 24.295999999999999],
  [183.17400000000001, 84.102999999999994, 16.613],
  [191.03200000000001, 86.813999999999993, 8.7279999999999998],
  [200.02199999999999, 91.299000000000007, 1.5]],
 296: [[229.304, 175.77099999999999, 24.684000000000001],
  [234.089, 176.70699999999999, 20.484999999999999],
  [237.922, 178.90100000000001, 19.071999999999999],
  [248.79400000000001, 174.82599999999999, 20.556999999999999],
  [255.565, 174.834, 17.895]],
 297: [[308.44299999999998, 47.625, 1.5],
  [310.86799999999999, 52.442999999999998, 9.4619999999999997],
  [307.30599999999998, 60.476999999999997, 16.391999999999999],
  [303.84500000000003, 64.304000000000002, 19.663],
  [302.12299999999999, 65.210999999999999, 24.094999999999999]]}

修改

回答我的问题,我想我也可以写:

def assignCoordinates_to_Cells(self,r):
    tracks={}
    allCoo=self.Coordinates
    for i in range(len(allCoo)):
        trackNumber=int(allCoo[i][4]-1000000000)
        track = np.vstack((allCoo[i][0:3],tracks.get(trackNumber,allCoo[i][0:3])))
        tracks[trackNumber] = track

1 个答案:

答案 0 :(得分:1)

np.concatenate两个1维数组仍会产生1维数组。您可以使用np.vstack垂直堆叠它们。使用不存在的密钥从dict获取值将返回None

def assignCoordinates_to_Cells(self,r):
    tracks={}
    allCoo=self.Coordinates
    for i in range(len(allCoo)):
        trackNumber=int(allCoo[i][4]-1000000000)
        track = tracks.get(trackNumber)
        if track is None:
            track = allCoo[i][0:3]
        else:
            track = np.vstack((allCoo[i][0:3],track))
        tracks[trackNumber] = track