2017-04-12 64 views
0

我遇到以下問題。我想將numpy數組連接到字典中的鍵。但我總是得到錯誤:「所有的輸入數組必須具有相同的維數」。到目前爲止我的代碼:將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

np.concatenate兩個1維數組仍然會產生1維數組。您可以使用np.vstack垂直堆疊它們。使用不存在的密鑰從字典獲取價值將返回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 
+0

嗯謝謝你的努力:)。我想我也可以像編輯它那樣編寫它。 – Varlor