1

I want to define the values of first row in a sorted table with search cursor. My code is working without sorting, but I don't know how can I integrate into sorting. I would like to see only the values of first row ("src_ORIG_FID","nbr_ORIG_FID"), with the smallest area and longer length. I want to sort rows first by "src_Shape_Area" Ascending, then by LENGTH Descending. After sorting, if the "if statement" is done, the first good row is needed.

enter image description here

with arcpy.da.SearchCursor(tablaL,["src_ORIG_FID","nbr_ORIG_FID","src_Shape_Area","LENGTH","SUM"],"","","src_Shape_Area A;LENGTH D") as nCursor:
 for row in nCursor:
 if row [4]<50000.00 and row[3]> 0: #if it is TRUE
 for row in nCursor:
 orig1 = row[0] #only first row value is needed
 orig2 = row[1]
 break
 else: 
 break
asked Jun 21, 2021 at 11:46
4
  • The documented syntax looks different from what you are providing (sql_clause is a two-element list). It also includes the text Note: DISTINCT, ORDER BY, and ALL are only supported when working with databases. Commented Jun 21, 2021 at 11:56
  • How many rows are there in your table? Commented Jun 21, 2021 at 12:18
  • More than 100 rows, and proximately 50% is out of "if statement". Commented Jun 21, 2021 at 12:20
  • Yes, it is in file geodatabase. I want to sorting, but the "if statement" is not come true go further the second row,etc... Vince what is your recomendation to solve this? Commented Jun 21, 2021 at 12:28

1 Answer 1

3

If you only have ~100 rows you can list all values, and then sort using python:

import arcpy
fc = r'C:\GIS\ArcMap_default_folder\Default.gdb\my_sample'
fields = ['OBJECTID', 'KKOD', 'Shape_Length', 'Shape_Area']
data = [row for row in arcpy.da.SearchCursor(fc, fields) if row[2]>0 and row[3]<50000] #List all rows meeting the if conditions
data.sort(key=lambda x: (x[2], -x[3])) #Sort by field index 2 and 3 (-3 for descending sort)
orig1, orig2 = data[0][:2] #Fetch first and second value of the first row in the sorted list of tuples
answered Jun 21, 2021 at 12:24
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.