Try this (edited per @alpha-beta-soup comment):
def myFunction (inter, prop, patch, shp, nat, edge):
myList = [inter, prop, patch, shp, nat, edge]
myList.sort()
myDict = {'Interconnectivity' : inter,
'PropCover' : prop,
'PatchSize' : patch,
'ShapeIndex': shp,
'Naturalness' : nat,
'EdgeNat' : edge}
myVal = 101 # using 101 as a max + 1 possible value; adjust if needed
myReturn = ''
for k, v in myDict.iteritems():
if v < myVal:
myVal = v
myReturn = k
return myReturn
Note that if you have two matching low values, there's nothing to select one over the other. That would require additional checking based on whatever your criteria is. Since a dictionary is unordered, you can't control it that way.
Here's another way without dictionaries, with an optional clause for ties. You may want different logic in case of ties.
def myFunction2 (inter, prop, patch, shp, nat, edge):
# use tuples to ensure value order
myScoreTuple = (prop, patch, shp, nat, edge)
myTextTuple = ('PropCover',
'PatchSize',
'ShapeIndex',
'Naturalness',
'EdgeNat')
# initialize values
myLowValue = inter
myReturn = 'Interconnectivity'
# now check for any lower value
for i in range(len(myScoreTuple)):
if myScoreTuple[i] < myLowValue:
myLowValue = myScoreTuple[i]
myReturn = myTextTuple[i]
# optional - add multiple text values in case of tie. Remove if unneeded
elif myScoreTuple[i] == myLowValue:
myReturn = ', '.join((myReturn, myTextTuple[i]))
return myReturn
You can use List comprehension with an if statement and then join the list elements together to a string with newline in between:
def FindLabel ( [Test1],[Test2],[Test3] ):
values = [Test1],[Test2],[Test3]
date = "05/10/2018"
labellist = ["{0} - {1}".format(date, value) for value in values if value != '0']
label = "\n".join(labellist)
return label
Best Answer
This function doesn't need to be too complex. Convert the items you want into a list, then join the result.
If this were proper Python, you would normally use
*args
to accept any number of arguments into a list.