Find max value in a field.

3757
7
Jump to solution
10-13-2017 01:42 PM
Highlighted
Occasional Contributor

Why do I get a print out of all the records values as opposed to just the highest which should be 8182 ?

with arcpy.da.SearchCursor("escroads",'RecID') as cursor:
    for row in cursor:
        print str(max(row))
Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
MVP Esteemed Contributor
field_vals = [1, 1000, 10, 100, 100]

search_cursor = [row_ for row_ in field_vals]

max(search_cursor)
Out[4]: 1000‍‍‍‍‍‍‍‍‍‍‍‍

you have to start thinking of you search cursor in this way.

or this way.

big_num = -1e200

for row_ in field_vals:
    print("{} bigger than {}?".format(row_, big_num))
   
1 bigger than -1e+200?
1000 bigger than -1e+200?
10 bigger than -1e+200?
100 bigger than -1e+200?
100 bigger than -1e+200?
‍‍‍‍‍‍‍‍‍‍

or do something as you go along

for row_ in field_vals:
    print("{} bigger than {}?".format(row_, row_*20))
   
1 bigger than 20?
1000 bigger than 20000?
10 bigger than 200?
100 bigger than 2000?
100 bigger than 2000?

View solution in original post

7 Replies
Highlighted
Occasional Contributor

Calling max() on a row will return the maximum value within the fields in that row, not the maximum across multiple rows.

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor
field_vals = [1, 1000, 10, 100, 100]

search_cursor = [row_ for row_ in field_vals]

max(search_cursor)
Out[4]: 1000‍‍‍‍‍‍‍‍‍‍‍‍

you have to start thinking of you search cursor in this way.

or this way.

big_num = -1e200

for row_ in field_vals:
    print("{} bigger than {}?".format(row_, big_num))
   
1 bigger than -1e+200?
1000 bigger than -1e+200?
10 bigger than -1e+200?
100 bigger than -1e+200?
100 bigger than -1e+200?
‍‍‍‍‍‍‍‍‍‍

or do something as you go along

for row_ in field_vals:
    print("{} bigger than {}?".format(row_, row_*20))
   
1 bigger than 20?
1000 bigger than 20000?
10 bigger than 200?
100 bigger than 2000?
100 bigger than 2000?

View solution in original post

Highlighted
Occasional Contributor

Thanks

Interesting, this is the most I have used cursors.

I get the desired result now, including the comma for some reason

with arcpy.da.SearchCursor("escroads",'RecID') as cursor:
    for row in cursor:
        print max(cursor)

(8182,)

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

max of a tuple I suspect

max((8131,))
Out[16]: 8131

check whether it works on the RecID might not be good if they are sorted...

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

Your existing syntax risks not providing the correct answer.  Your syntax should be:

with arcpy.da.SearchCursor("escroads",'RecID') as cursor:
    print max(cursor)

The Python max function is designed to work on iterables, which a Python cursor is a form of an iterable.  Your original code sets up the cursor/iterable, but the for loop moves the iterator forward one position, and then max is called against the remainder of the iterable, exhausting the cursor.  If the maximum value just happens to be the first record, it will not be returned as the maximum value because max never knew the value existed.

Cursors are extremely powerful, I encourage you to learn more about them and also Python iterables.  I started (but haven't finished yet) an "Iterable Cursor" blog series that might help someone new to ArcPy cursors understand how to think of them as Python iterables: /blogs/tilting/2015/11/24/the-iterable-cursor-iteration-basics.

Highlighted
Occasional Contributor

Invaluable advice I will look into this more.

Thanks.

Reply
0 Kudos
Highlighted
Occasional Contributor II

I took this concept a step or two further and developed this little one-line gem which uses list comprehension and treats the cursor like a tuple:

max([cur[0] for cur in arcpy.da.SearchCursor("Asset Layer", "ASSETID")])

Important notes: This bit of arcpy code works in ArcMap, but it fails on an empty layer because the list is empty.  Also, since it's in ArcMap, the code above honors the user's selected rows or features and therefore returns the maximum number of the field within the selection.

Reply
0 Kudos