Python Add max(searchCursor) values to Dictionary

443
4
Jump to solution
04-19-2019 09:09 AM
Business_IntelligenceSoftware
Occasional Contributor

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:
    print(market)

    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 = arcpy.mp.ArcGISProject(r'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:
    
                try:
    
                    sqlClause = (None, 'ORDER BY ' + 'SUM_USER_VisitCount')
        
                    with arcpy.da.SearchCursor(in_table = Layer, field_names = fields, 
                    sql_clause = sqlClause) as searchCursor:
                        print(max(searchCursor))
                        
                        results_dict[Layer] = max(searchCursor)
                        
                except:
                    pass


print(results_dict)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

You will return a tuple when searching for the max value, i.e:

(100,)

Try the following:

with arcpy.da.SearchCursor(in_table = Layer, field_names = fields, sql_clause = sqlClause) as searchCursor:
    for val in max(searchCursor):
        results_dict[Layer] = int(val)
del searchCursor

View solution in original post

4 Replies
JakeSkinner
Esri Esteemed Contributor

You will return a tuple when searching for the max value, i.e:

(100,)

Try the following:

with arcpy.da.SearchCursor(in_table = Layer, field_names = fields, sql_clause = sqlClause) as searchCursor:
    for val in max(searchCursor):
        results_dict[Layer] = int(val)
del searchCursor
Business_IntelligenceSoftware
Occasional Contributor

This worked! Thank you Jake

0 Kudos
Luke_Pinner
MVP Regular Contributor

And don't forget that you can only use a cursor once and then it's exhausted/empty. So by using 

print(max(searchCursor))

You have exhausted the cursor.

Business_IntelligenceSoftware
Occasional Contributor

I was not aware of that. Thank you!

0 Kudos