Hi John,
Matthew and Stephen suggestions are right on track. Either using the Search Cursor, or the Get Count will be identical in performance. Example using Search Cursor:
mxd = arcpy.mapping.MapDocument(r"C:\Projects\\Municipality.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] list = [] with arcpy.da.SearchCursor(lyr, ["OID@"], "OBJECTID > 0") as cursor: for row in cursor: list.append(row[0]) del cursor count = len(list) if count == 0: ..... del mxd
Example using Get Count:
mxd = arcpy.mapping.MapDocument(r"C:\Projects\Municipality.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] for lyr in arcpy.mapping.ListLayers(mxd, "*", df): result = arcpy.GetCount_management(lyr) count = int(result.getOutput(0)) if count == 0: ..... del mxd
Both techniques will/do work, but in my experience the performance is not identical between the two. For small to medium datasets (~100,000 records or less), the performance can be quite similar with the arcpy.da.SearchCursor and arcpy.GetCount_management. For larger datasets, I have seen GetCount outperform cursors by 3 to 10 times.
Jason Scheirer speaks to why using a GP tool is faster than a Python cursor with larger datasets: Re: How to determine number of records using arcpy.da.SearchCursor.
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.