rows = arcpy.SearchCursor(table,searchstring) #count the number of rows in output count = 0 for row in rows: count += 1
obviously, I need to get out of the python window in ArcGIS 10
dict = {} for item in range(1,1500000): dict[item] = random.uniform(1,10) #return a random float count = 0 for item in dict: if dict[item] > 5: count = count + 1 print count >>> 833384
suggestion 1, I don't think I can use SelectLayerByAttribute because I have a standalone table
Re: suggestion 2, I thought this might be the winner, since I do have a series of searches to run over the same table.
#v9.3 code BTW dict = {} searchRows = gp.searchcursor(tblPath) searchRow = searchRows.next() while searchRow: dict[searchRow.MY_KEY] = [searchRow.FIELD1,searchRow.FIELD2] #this could be a tuple instead of a list to save memory if you didn't plan on changing the values... searchRow = searchRows.next() del searchRow del searchRows #yes, there's probably a faster/more elegant way to do this... Kim? .iteritems or .itervalues any faster? rowCounter1 = 0 rowCounter2 = 0 for myKey in dict: if dict[myKey][0] == 100 or dict[myKey][1] == "dog": rowCounter1 = rowCounter1 + 1 if dict[myKey][0] == 200 or dict[myKey][1] == "cat": rowCounter2 = rowCounter2 + 1 print rowCounter1 print rowCounter2
@clockit def do_search_by_numpy(table): my_array = numpy.fromiter(countrows(table),dtype=numpy.dtype(float)) count = 0 for i in range(100,1000,100): count += numpy.size(where(my_array < i)) print "done with dict: " + str(count) return count @clockit def countrows(table): searchRows = arcpy.SearchCursor(table) for myrow in searchRows: yield myrow.NEAR_DIST
It is already a list after a split
from arcpy import * from stopwatch import clockit from numpy import * @clockit def do_search_by_count(table): count = 0 for i in range(100,1000,100): searchstring = "NEAR_DIST < " + str(i) count += make_table_and_count(table, searchstring) print "done by count: " + str(count) return count @clockit def do_search_by_cursor(table): count = 0 for i in range(100,1000,100): searchstring = "NEAR_DIST < " + str(i) count += cursor_search_count(table,searchstring) print "done by cursor: " + str(count) return count @clockit def do_search_by_dict(table): lookupDict = create_dict(table) count = 0 for i in range(100,1000,100): count += count_dict(lookupDict,i) print "done with dict: " + str(count) return count @clockit def make_table_and_count(table,searchstring): arcpy.MakeTableView_management(table,"mytv5k",searchstring) x = int(str(arcpy.GetCount_management("mytv5k"))) return x @clockit def cursor_search_count(table,searchstring): rows = arcpy.SearchCursor(table,searchstring) count = 0 for row in rows: count += 1 return count @clockit def create_dict(table): lookupDict = {} searchRows = arcpy.SearchCursor(table) for myrow in searchRows: lookupDict[myrow.OBJECTID] = [myrow.NEAR_DIST] del myrow del searchRows print "created dict" return lookupDict @clockit def count_dict(lookupDict,dist): count =0 for k,v in lookupDict.iteritems(): if v[0] < dist: count += 1 return count
gp.MakeFeatureLayer_management(fc, "fl") gp.SelectLayerByAttribute_management("fl", "NEW_SELECTION", "WIDGETS > 30") fidSet = gp.describe("fl").fidset #this returns a big honking text string - can be millions of charatcters long in my experience! selectedOidList = [] for fid in fidSet.split(";"): selectedOidList.append(fid) print len(selectedOidList)
from arcpy import * from stopwatch import clockit clockit def maketv_count(table): arcpy.MakeTableView_management(table,"mytv5k","NEAR_DIST < 1000") arcpy.GetCount_management("mytv5k")
def maketv_cursor(table): rows = arcpy.SearchCursor(table,"NEAR_DIST < 1000") count = 0 for row in rows: count += 1
gp.MakeFeatureLayer_management(indxTileFC, indxTileFL, "FID_local_know_nw > -1") if int(gp.GetCount_management(indxTileFL).getoutput(0)) > 0: gp.Blah_managment(this, that, theOther)
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.