Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将numpy数组连接到字典中的现有键_Python_Arrays_Numpy_Dictionary - Fatal编程技术网

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

Python 将numpy数组连接到字典中的现有键,python,arrays,numpy,dictionary,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)

我有以下问题。我想将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.坐标的形状如下:

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

np.连接两个一维数组仍将生成一维数组。您可以使用
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
啊,谢谢你的努力:)。我想我也可以像编辑一样写。