Select to view content in your preferred language

RE: absolute minimum value selection

482
5
Jump to solution
08-08-2012 08:07 AM
LarissaPizzolato
Emerging Contributor
Hello,

I have a file geodatabase table with a field titled "min_value". Within this field there are values from -1000 to +1000. I am trying to search all of the rows in the table to select the row with the lowest absolute value within the "min_value" field (ie. value +/- closest to zero). I will then be using a value (shapefile name) within the same row (but different field) to identify which shapefile to use in the next processing step of the script.

Any help is greatly appreciated!

Thank-you!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
There may be a better way, but here's the best solution I could find.

curs =  arcpy.SearchCursor(table) for row in curs:     listing.append(row.min_value)  print min((abs(0 - i), i) for i in listing)[1]


You could also pass the OID or some other identifier to go back to the row object.

View solution in original post

0 Kudos
5 Replies
MathewCoyle
Honored Contributor
There may be a better way, but here's the best solution I could find.

curs =  arcpy.SearchCursor(table) for row in curs:     listing.append(row.min_value)  print min((abs(0 - i), i) for i in listing)[1]


You could also pass the OID or some other identifier to go back to the row object.
0 Kudos
curtvprice
MVP Esteemed Contributor
Matt - I guess I have a tweak:
tv = arcpy.MakeTableView(table)
curs =  arcpy.SearchCursor(tv)
listing = list() # create a list
for row in curs:
    listing.append(row.min_value)
del row, curs # always a good idea when done!
xmin = min([abs(i) for i in listing])
# now get that record (or the first if several) with that abs value
curs = SearchCursor(tv,"abs(%s) = %s" % (min_value,xmin))
# get the value for field "shapefile"
shapefile = curs.next().shapefile
del curs
0 Kudos
MathewCoyle
Honored Contributor
Matt - I guess I have a tweak:
tv = arcpy.MakeTableView(table)
curs =  arcpy.SearchCursor(tv)
listing = list() # create a list
for row in curs:
    listing.append(row.min_value)
del row, curs # always a good idea when done!
xmin = min([abs(i) for i in listing])
# now get that record (or the first if several) with that abs value
curs = SearchCursor(tv,"abs(%s) = %s" % (min_value,xmin))
# get the value for field "shapefile"
shapefile = curs.next().shapefile
del curs


Looks good, much more fleshed out. Maybe I am missing something though, this doesn't seem to preserve the negative sign with the xmin assignment.
0 Kudos
curtvprice
MVP Esteemed Contributor
Looks good, much more fleshed out. Maybe I am missing something though, this doesn't seem to preserve the negative sign with the xmin assignment.


You're right--it doesn't preserve the sign.

I don't think you need it as long as you use the ABS function in the second SearchCursor SQL expression.

Arc 10 help: SQL reference for query expressions used in ArcGIS
0 Kudos
MathewCoyle
Honored Contributor
Ah yes, makes sense. Doesn't really matter if they are positive or negative if they are the same absolute value anyways.
0 Kudos