# Extracting field names for second and third highest values in a row

277
1
02-14-2022 07:12 AM
by
New Contributor

Hi, I have managed to extract the highest, second and third highest value, aswell as the field name of the highest value in a row. However, I´m struggling to extract the field name of the second and third highest value in a row. I used the following python code to extract highest, second and third highest values, and field name of highest:

Highest value:

maxnum([!Field1!, !Field2!, !Field3!,…])

def maxnum(fields):
fields.sort()
return fields[-1]

Field name with the highest value:

getMaxField(!Field1!,!Field2!,!Field3!)

def getMaxField(v1, v2, v3, name1, name2, name3):
maxval = max(v1,v2,v3)
if maxval == v1:
return name1
if maxval == v2:
return name2
return name3

Any suggestions on how I can rewrite the code, or use a different code, to extract field names of second and third highest value?

Tags (2)
1 Solution

Accepted Solutions
MVP Esteemed Contributor

By virtue of example to change max into something else

``````# -- sort then slice
v1= 10
v2=3
v3=20
sorted([v1, v2, v3], reverse=True)
[20, 10, 3]
# -- now slice what you want
sorted([v1, v2, v3], reverse=True)[1:]
[10, 3]
# --
sorted([v1, v2, v3], reverse=False)[:-1]
[3, 10]
# -- you can put ranges on the limits eg [1:4] etc)``````

... sort of retired...
MVP Esteemed Contributor

By virtue of example to change max into something else

``````# -- sort then slice
v1= 10
v2=3
v3=20
sorted([v1, v2, v3], reverse=True)
[20, 10, 3]
# -- now slice what you want
sorted([v1, v2, v3], reverse=True)[1:]
[10, 3]
# --
sorted([v1, v2, v3], reverse=False)[:-1]
[3, 10]
# -- you can put ranges on the limits eg [1:4] etc)``````

... sort of retired...