cursor compare last und actual row

4262
8
Jump to solution
09-10-2015 05:25 AM
UrsRichard
New Contributor II

How can I iterate trough a cursor an compare the last row with the actual?

def disolveOemDolungen(bnum):

    unterlieger = None

    pCursor = arcpy.UpdateCursor(oemFC, "BNUM = '" + bnum + "'","","von; bis; klasse, key","von A; bis A")

    row = pCursor.next()

    while row:

        if not unterlieger is None:

            if (unterlieger.getValue("klasse") == 7):

                arcpy.AddMessage("Oberlieger:" + unterlieger.getValue("key"))

                if (math.fabs(unterlieger.getValue("bis") - row.getValue("von")) <= 1):

                    unterlieger.setValue("klasse", row.getValue("klasse"))

                    unterlieger.setValue("sbreite", row.getValue("sbreite"))

                    pCursor.updateRow(unterlieger)

                    arcpy.AddMessage("Dissolve mit Oberlieger: " + unterlieger.getValue("key"))

        if row.getValue("klasse") == 7:

            if not unterlieger is None:

                arcpy.AddMessage("Unterlieger " + row.getValue("key"))

                if (math.fabs(unterlieger.getValue("bis") - row.getValue("von")) <= 1):

                    row.setValue("klasse", unterlieger.getValue("klasse"))

                    row.setValue("sbreite", unterlieger.getValue("sbreite"))

                    pCursor.updateRow(row)

                    arcpy.AddMessage("Dissolve mit Unterlieger: " + row.getValue("key"))

        unterlieger = row

        row = pCursor.next()

Row and unterlieger contains always the same Object. Therefore I can't compare the rows. How can this be done with Python?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ThomasEmge
Esri Contributor

It is because of the type of row the arcpy.UpdateCursor is returning. Have you looked into the data access module?

http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-data-access/what-is-the-data-access-module...

The da module offers better performance as well as a more pythonic experience. For example it is returning the rows as dictionaries.

So this would work:

>>> with arcpy.da.UpdateCursor("mydata", "*") as cursor:
...     old = None
...     for row in cursor:
...         if old is not None:
...             print row[0], old[0]
...         old = row

View solution in original post

8 Replies
WesMiller
Regular Contributor III

Are you wanting to compare attributes from row to row? Read a row into a list or dictionary then when you get to the next row compare and repeat.

0 Kudos
UrsRichard
New Contributor II

In Some cases I have to store values in the "last" Row. So the Values in a List are not enough. I need the Row object.

0 Kudos
WesMiller
Regular Contributor III

Have you looked at make feature layer as an option to hold the single row? You could hold it row by row or multiple rows if you needed.

0 Kudos
UrsRichard
New Contributor II

Could you give me an example how I can get hold of multiple rows with make fature layer?

0 Kudos
WesMiller
Regular Contributor III
import arcpy
fc = "Your Feature Class"
desc = arcpy.Describe(fc)
oidfld = desc.OIDFieldName
fld = arcpy.AddFieldDelimiters(fc,oidfld)
print oidfld,fld
with arcpy.da.SearchCursor(fc, ("OID@")) as cursor:
    for row in cursor:
        tmplyr = "tmplyr"+str(row[0])
        #You could create a list of features here to input below
        query = fld + " in (" + str(row[0]) + ")"
        arcpy.MakeFeatureLayer_management(fc,tmplyr,query)
0 Kudos
ThomasEmge
Esri Contributor

It is because of the type of row the arcpy.UpdateCursor is returning. Have you looked into the data access module?

http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-data-access/what-is-the-data-access-module...

The da module offers better performance as well as a more pythonic experience. For example it is returning the rows as dictionaries.

So this would work:

>>> with arcpy.da.UpdateCursor("mydata", "*") as cursor:
...     old = None
...     for row in cursor:
...         if old is not None:
...             print row[0], old[0]
...         old = row
UrsRichard
New Contributor II

Create! That's exactly what I was lookinf for. Thanks a lot!

0 Kudos
RhettZufelt
MVP Frequent Contributor

Not exactly what you need, but I've used this to look for missing and/or duplicate values in the field [recnum], and if there are blanks or duplicates, replace with the next sequential number.

Like I said, not exactly what you are after, but I had this and thought you might get some ideas from it.


R_

def FixDuplicates():
    global rec
    rec = 0
    maxValue = arcpy.SearchCursor(infc, "", "", "", "recnum D").next().getValue(myField) #Get 1st row in descending cursor sort
    if maxValue == None:
        maxValue = 0
    newValue = maxValue + 1
    uCur = arcpy.UpdateCursor(infc, "", "", "", "recnum A")
    for row in uCur:
        if row.getValue('recnum')  and row.getValue('recnum') != 0:
                  curRec = row.getValue('recnum')
                  #First if checks
                  #if the row's rec is equal to the previous row
                  if curRec != rec:
                      rec = row.getValue('recnum')
                  #If its rec is not
                  #equal to the previous row then we are ready to update the existing row
                  else:
                      rec = row.getValue('recnum')
                      row.recnum = newValue
                      newValue += 1
                      uCur.updateRow(row)
        else:
          rec = row.getValue('recnum')
          row.recnum = newValue
          newValue += 1
          uCur.updateRow(row)
    del uCur

try:
          infc = r'Database Connections\s14-arcgis-p_OS.sde\PublicWorks.DBO.Sewer_Network\PublicWorks.DBO.ssGravityMain'
          FixDuplicates()