maximum of list doesn't work, any ideas why?

374
3
Jump to solution
08-20-2020 09:54 PM
JohannesBierer
Regular Contributor

Hello,

I want to select the maximum area of features with the same attribute. If I run the following script I don't get the maximum value of the list. Any ideas why?

import arcpy

arcpy.env.overwriteOutput = True

inFile = r"path... \Default.gdb\NSG_Kreis_Inters"
fc = r"path... \Bearbeitung.gdb\data\Test_NSG_Hauptkreis"
Fields = ["OBJECTID_1", "OBJEKT", "m2"]

inFileL = "inFileL"

arcpy.CopyFeatures_management(inFile, fc)
arcpy.MakeFeatureLayer_management(fc, inFileL)
arcpy.AddField_management(inFileL, "relevant", "Single")
arcpy.AddField_management(inFileL, "m2", "Long")

arcpy.CalculateField_management(inFileL, "m2", "!Shape_Area!", "PYTHON_9.3")

with arcpy.da.SearchCursor(inFileL, Fields) as cursor:
    for row in cursor:

        sql = "OBJEKT = '{}'".format(row[1].encode('utf-8'))
        arcpy.SelectLayerByAttribute_management(inFileL, where_clause=sql)

        myList = list()

        with arcpy.da.SearchCursor(inFileL, Fields) as cursor:
            for row in cursor:
                
                myList.append("{}".format(row[2]))
                
        print myList
        print max(myList)
                
        sql1 = "{} AND m2 = {}".format(sql, max(myList))
        
        arcpy.SelectLayerByAttribute_management(inFileL, "NEW_SELECTION", where_clause=sql1)
        arcpy.CalculateField_management(inFileL, "relevant", 1, "PYTHON_9.3")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Here is the result of the prints in lines 31 and 32:

['0', '1012219']
1012219
['0', '1012219']
1012219
['2', '1344613']
2
['2', '1344613']
2
['1649705', '2100']
2100
['1918769', '2100']
2100

0 Kudos
1 Solution

Accepted Solutions
JamesBrander
Esri Contributor

Hi Johannes Bierer‌,

It looks like the values being returned are all strings which is confusing the issue, as the max() function behaves differently for strings and numbers. What we really want to know is which value is largest once those strings are converted into the integers, so one way you could get around this would be to use a list comprehension to generate a new list with the values converted to integers and run the max function for the new list. 

intList = [int(value) for value in myList]
max(intList)‍‍‍‍

Hope that helps.

Regards,

James

View solution in original post

3 Replies
JamesBrander
Esri Contributor

Hi Johannes Bierer‌,

It looks like the values being returned are all strings which is confusing the issue, as the max() function behaves differently for strings and numbers. What we really want to know is which value is largest once those strings are converted into the integers, so one way you could get around this would be to use a list comprehension to generate a new list with the values converted to integers and run the max function for the new list. 

intList = [int(value) for value in myList]
max(intList)‍‍‍‍

Hope that helps.

Regards,

James

JohannesBierer
Regular Contributor

Thank you, that helped a lot.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

For the sake of learning, I want to point out that the Python max function takes any iterable. You don't have to rely on creating an intermediate list, you can create a generator expression within the function call directly.

>>> str_list = ['1649705', '2100']
>>> 
>>> max(str_list)
'2100'
>>> 
>>> max(int(i) for i in str_list)
1649705
>>>