0

I'm working through some da.UpdateCursor scripts to make updates to a fgdb. Although the script below works successfully as a rule, it returns the incorrect max value from one feature class.

The feature class in question has been rebuilt numerous times but I still get the second highest value returned not the highest. Note this occurs in this feature class on the three columns with values (OBJECTID, Shape_Length, Shape_Area), the only other column is Shape.

Can anyone provide some guidance please as I have researched this and can't get an answer after some hours of experimenting across numerous other feature classes.

My only thought at this point, 'is there a ceiling on max?' given that min(cursor) returns the lowest value correctly on the three columns.

Using Win 7, ArcGIS 10.2, Python 2.7.5 (yes, I'm in a mixed OS environment).

    import arcpy, sys, os
    from os import sep


    ws = env.workspace

    with arcpy.da.UpdateCursor(ws +sep+ 'Library_362', "Shape_Area") as cursor:
        for row in cursor:
            highValue = max(cursor)
            print highValue

    # returns [7693891.489877664]
    # s/be   [10054353.128549]
  • It sounds crazy but the script works fine, i.e. it prins only once and produces correct answer! Moreover it's enough to move highvalue=... after with statement. No need to iterate – FelixIP Oct 13 '15 at 5:21
5

can't test it at the moment, though i'm surprised a cursor can/should be accessed that way? i would think you would want something like:

highValue = 0.0
with arcpy.da.UpdateCursor(ws +sep+ 'Library_362', "Shape_Area") as cursor:
    for row in cursor:
        if row[0] > highValue:
            highValue = row[0]
print highValue # NOTE: print this at end of loop, rather than inside.
  • Yes that works fine thanks @fluidmotion. It does however create a further mystery for me in that the script I wrote works for all fields I tested in another 20 feature classses in my fgdb and also another 20 or so in a bunch of other fgdb. Puzzling. – user1995458 Oct 13 '15 at 3:46
  • hmmm. As @FelixIP mentioned, the max() could conceivably work - without iterating (actually, the docs state that max() accepts an iterable (cursor), not just a list!)...could it be that max is producing the string order rather than the numerical order in some cases? – fluidmotion Oct 13 '15 at 11:08
1

Your method may work, but it will perform the same action (finding the max in the cursor) for each row.

Here's a version that utilizes list comprehension:

highValue = max ([val for val, in arcpy.da.UpdateCursor
                  (ws +sep+ 'Library_362', "Shape_Area")])
  • Instead of creating a potentially extremely large list in memory, you can pass a generator expression directly to sum, max, min, etc. For example, sum(x*x for x in xrange(20)) – Paul Oct 14 '15 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.