Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

UpdateCursor with nested loops and SelectLayerByAttributes

$
0
0
Hi,

I need to update fields in a feature class based on values from another feature class (with different geometry). Some of the features have multiple values and are handled in a separate loop to the other values.

I have hacked something together that works with my test data, but using multiple SelectLayerByAttribute to get values from the other feature class makes it run much slower than I think it should. There must be a Python data structure I can use instead (like a dictionary with FID as key and LandCover as value) that will be faster and make my code more 'Pythonic'.

Any suggestions appreciated!

Code:

with arcpy.da.UpdateCursor(paddock, fields) as cursor:
    for row in cursor:
        #print row
        for i in singleList:
            #print i
            if row[0] == i[0]:
                arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", """"FID" = {0}""".format(i[0]))
                landCover = list(r.LandCover for r in arcpy.SearchCursor(fc))
                #print "updating.... " + landCover[0]
                row[1] = landCover[0]
                cursor.updateRow(row)
        for i in dupList:
            #print i
            if row[0] == i[0]:
                arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", """"FID" = {0}""".format(i[0]))
                landcoverList = list((r.LandCover,r.Shape_Area) for r in arcpy.SearchCursor(fc))
                maxlandCover = max(landcoverList, key=lambda x: x[1])
                landCover = list(maxlandCover)[0]
                #print "updating.... " + landCover
                row[1] = landCover
                cursor.updateRow(row)

Thanks,

Matt

Viewing all articles
Browse latest Browse all 2485

Trending Articles