Finding max value in field using ArcPy

arcpycursorstatistics

I have a table and I want to find max and min value in specified field (f1) with their names. I used this code but I only could print max and min values in f1. How can I print their names Like the image I put I want to print min value of f1 and its name net72.shp. I think it must be using WHERE_CLAUSE.

table

import arcpy
fc = r"E:\gis payannameh\tabusearch2\SUMMARY.gdb\net38_j"
from arcpy import env
fields = ['f1','FIRST_netw']
all_rows = [i[0] for i in arcpy.da.SearchCursor(fc,fields[0])]
min_val = min(all_rows)
print min_val

Best Answer

List all values, use min/max with a lambda function:

import arcpy
fc = r'C:\GIS\ArcMap_default_folder\Default.gdb\my_sample'
fields = ['OID@', 'Shape_Area']

#List all rows
allvalues = [row for row in arcpy.da.SearchCursor(fc, fields)] #[(1, 66901.86709246939), (2, 16444.625346498582), (3, 161485.720859425), ...]

#min(allvalues, key=lambda x: x[1]) #x[1] is to search by second value, in my case 'Shape_Area'
#(105, 1311.0614255410774)

#max(allvalues, key=lambda x: x[1])
#(43, 35160520.119611494)

Or you can sort the list and fetch first and last value using indexes:

allvalues.sort(key=lambda x: x[1])
allvalues[0] #min
allvalues[-1] #max