Skip to main content
Added another method
Source Link
recurvata
  • 2.3k
  • 15
  • 22

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

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.

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
Edited to improve answer per comment
Source Link
recurvata
  • 2.3k
  • 15
  • 22

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() 
   # ordermyDict low= to{'Interconnectivity' high: inter,
    myDict = {inter        'PropCover' : 'Interconnectivity'prop,
              prop'PatchSize' : 'PropCover'patch,
              patch 'ShapeIndex': 'PatchSize'shp,
              shp'Naturalness' : 'ShapeIndex'nat,
              nat'EdgeNat' : 'Naturalness',edge}

    myVal = 101   # using 101 as a max + 1 edgepossible value; adjust if needed
    myReturn = ''
    for k, v in myDict.iteritems(): 
 'EdgeNat'}       if v < myVal:
            myVal = v
            myReturn = k

    return myDict[myList[0]]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.

Try this:

def myFunction (inter, prop, patch, shp, nat, edge):
    myList = [inter, prop, patch, shp, nat, edge]
    myList.sort()   # order low to high
    myDict = {inter : 'Interconnectivity',
              prop : 'PropCover',
              patch : 'PatchSize',
              shp : 'ShapeIndex',
              nat : 'Naturalness',
              edge : 'EdgeNat'}

    return myDict[myList[0]]

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.

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.

Added note about matching values
Source Link
recurvata
  • 2.3k
  • 15
  • 22

Try this:

def myFunction (inter, prop, patch, shp, nat, edge):
    myList = [inter, prop, patch, shp, nat, edge]
    myList.sort()   # order low to high
    myDict = {inter : 'Interconnectivity',
              prop : 'PropCover',
              patch : 'PatchSize',
              shp : 'ShapeIndex',
              nat : 'Naturalness',
              edge : 'EdgeNat'}

    return myDict[myList[0]]

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.

Try this:

def myFunction (inter, prop, patch, shp, nat, edge):
    myList = [inter, prop, patch, shp, nat, edge]
    myList.sort()   # order low to high
    myDict = {inter : 'Interconnectivity',
              prop : 'PropCover',
              patch : 'PatchSize',
              shp : 'ShapeIndex',
              nat : 'Naturalness',
              edge : 'EdgeNat'}

    return myDict[myList[0]]

Try this:

def myFunction (inter, prop, patch, shp, nat, edge):
    myList = [inter, prop, patch, shp, nat, edge]
    myList.sort()   # order low to high
    myDict = {inter : 'Interconnectivity',
              prop : 'PropCover',
              patch : 'PatchSize',
              shp : 'ShapeIndex',
              nat : 'Naturalness',
              edge : 'EdgeNat'}

    return myDict[myList[0]]

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.

Source Link
recurvata
  • 2.3k
  • 15
  • 22
Loading