AnsweredAssumed Answered

Python Add max(searchCursor) values to Dictionary

Question asked by bisoftware on Apr 19, 2019
Latest reply on Apr 25, 2019 by bisoftware

My code loops through layers in multiple projects and prints the highest values for a specific field. I want to then append those values to an empty dictionary, but when I try and print the dictionary at the end of the code it is still empty. 


I've been trying to figure this out for a couple days now, but can't seem to figure it out myself or find a solution online.


Any help or suggestions would be greatly appreciated. Thanks.


import arcpy
from arcpy import env

markets = [3000]

### Declare variables
fc = 'OpenCenters'
fields = ['USER_market_id','USER_ID','USER_Center_Name','USER_Opening_Date']
fieldname = 'USER_market_id'

sqlclause = (None, 'Order By USER_market_id, USER_ID')

for market in markets:

    results_dict = {}
    env.workspace = r"E:\arcGIS_Shared\Python\HeatMaps_{0}\HeatMaps_{0}.gdb".format(market)
    ## Define WHERE clause statement
    whereclause = """{} = """.format(arcpy.AddFieldDelimiters(fc, fieldname)) + str(market)
    ### Cursor to create list of centers to loop through
    with arcpy.da.SearchCursor(in_table = fc, field_names = fields, where_clause=whereclause, sql_clause=sqlclause) as cursor:
        ###Loop to run SummarizeWithin by center
        for row in (cursor):
            p ='E:\arcGIS_Shared\Python\HeatMaps_{0}\DiscreteHeatMaps{0}_{1}.aprx'.format(market, row[2]))
            m = p.listMaps()[0]
            Layers = m.listLayers("DiscreteVisitsSumWithin{0}_{1}*".format(market, row[2]))

            fields = ['SUM_USER_VisitCount']

            for Layer in Layers:
                    sqlClause = (None, 'ORDER BY ' + 'SUM_USER_VisitCount')
                    with arcpy.da.SearchCursor(in_table = Layer, field_names = fields,
                    sql_clause = sqlClause) as searchCursor:
                        results_dict[Layer] = max(searchCursor)