0

I couldn't find this question, although I am sure it has been asked.

How could I use a python list to condense this script? This code block currently works, but is clearly not very efficient.

def Reclass(test):
if test == '2 7':
    return 'Padmounted Single Phase'
elif test == '2 4':
    return 'Padmounted Three Phase'
elif test == '2 2':
    return 'Padmounted Three Phase'
elif test == '2 1':
    return 'Padmounted Three Phase'
elif test == '1 4':
    return 'OH Single Phase'
elif test == '1 2':
    return 'OH Single Phase'
elif test == '1 1':
    return 'OH Single Phase'
elif test == '1 5':
    return 'OH Second Phase'
elif test == '1 3':
    return 'OH Second Phase'
elif test == '1 7':
    return 'OH Third Phase'

I was attempting to use (as an example of a list):

elif test == ['1 1', '1 2', '1 3']

but the code was being skipped over and not working.

1
  • 1
    Technically I think this is a pure Python rather than GIS Question and would be better researched/asked at StackOverflow with references to the Field Calculator removed.
    – PolyGeo
    Commented May 2, 2014 at 22:28

2 Answers 2

5

elif test in ['1 1', '1 2', '1 3']

2

You could use a dictionary. Something like this:

test_dict = {'2 7':'Padmounted Single Phase', '2 4':'Padmounted Three Phase', ....}

then you can get the answer

def reclass(test):
   return test_dict[test]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.