如何获得矢量图层字段中两行之间的差异

How to get the difference between two rows in a vector layer field

因此该字段来自矢量图层属性table。我想要的是当名为“距离”的字段中的每一行值从前一个值中减去时能够得到结果,我得到一个结果,然后我可以将其用于其他计算。所以基本上我想说:第 4 列中的第 3 行减去第 4 列中的第 2 行(相同的列但不同的行相互减去)。我的代码如下所示:

   fn = ‘C:/PLUGINS1/sample/checking.shp’

   layer = iface.addVectorLayer(fn, ”, ‘ogr’)
   layer=iface.activeLayer()
   idx=layer.fields().indexFromName(‘Distance’)

   with edit(layer):
      for f in layer.getFeatures():
          dist1 = float(row[2], column [4]) # since row 1 contains the field name
          dist2 = float(row[3], column [4])
          final = abs(dist2 – dist1)

出现错误。我卡在这里了。

这确实有效:

   fn = 'C:/PLUGINS1/sample/checking.shp'
   layer = iface.addVectorLayer(fn, '', 'ogr') # '' means empty layer name
  
   for i in range(0, 1):
       feat1 = layer.getFeature(i)
       ab = feat1[0] # this is the first column and first row value
   for i in range(0, 2):
       feat2 = layer.getFeature(i)
       dk = feat2[0] # this is the first column and second row value
       lenggok = (dk - ab) # Note this is the difference between both rows 

   print(lenggok)