0

I have the following land use classes and they each have a key

1, 2, 3, 4, = [Arable]

5, 6, 7, = Non arable

8 = Protected

c = Climate

e = Erosion

s = Soil

w = Wetness

estu= Estuaries

ice = Ice

lake = Lake

quar = quarries/mines

rive = River

town = town/urba
  • I want to create a function that allows me to call multiple values ie if I entered in 4e15 it should return ‘Arable (4)’, ‘Erosion limited’, ‘15’, ‘’, ‘’, ‘’

  • If I entered an invalid code it should just return blacks

  • Finally if one of the non-nominal codes is entered the meaning of this code should be returned in positions 0, 1, 3, 4 in the output list, with blanks as the value in positions 2 and 5.

1
  • what have you tried so far ??..plz post the code ,it will help people to answer !! Commented May 10, 2016 at 6:34

2 Answers 2

2

Without fully understanding your usage, this looks like a good scenario for a Python dictionary:

table = {
    '1': 'Arable',
    'c': 'Climate',
    'e': 'Erosion',
    # etc.
}

table['e']

Outputs:

'Erosion'

That said, it seems the problem is not well defined. Inputing '15' could be interpreted as a 1, and then a 5, not as a unique 15, and there should be no way to differentiate given the input you currently have. It'll be easier if you start with a well-defined set of distinct input literals.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure if I interpreted your question correctly. Generally you can create a dictionary table as @Jameson mentioned. But to parse the 4e15 expression without delimiters that separate between the characters would make your function limited to your set of choices provided in your code and technically not practical for larger scales, but works for smaller (limited) ones. So let me show general ways to achieve this goal:

def get_vals(vals, table):
    res = []          
    for x in vals:
        res.append(table.get(x, 'error'))  # Or anything instead of error for invalid inputs
    return res 

>>> table = {'a':'test1', 'b':'test2'}  
>>> get_vals('ab', table)   
['test1', 'test2', 'error', 'error']  

This function parses only one single character per a string input, if you enter string "15" it'll try to fetch the value of "1" from the dictionary and then "5", not string "15" as a whole!

With the delimiter way, this will be more practical, easier to maintain and work with. For example:

def get_vals2(vals, table, delimiter = ','):      # Default: split string with comma or whatever
    res = [] 
    for x in vals.split(delimiter): 
        res.append(table.get(x, 'error')) 
    return res 

>>> get_vals2('1,a,15,b', table)
['error', 'test1', 'error', 'test2']

As you can see, if you input 1 and 5, it's "15" when parsed, unlike the prior version. Another option would be to list all the combinations that the user may input, but what a tedious work! For instance:

table = {'4e15': your values, 'e415': your values, '415e': your values, continuing with others as well...}

Very complex and you'll write a huge list of combinations and not efficient when it comes to maintenance, because you hard-coded many combinations, a small little change could result in re-writing all the combinations again!

4 Comments

I'm an amature at all of this sorry. So first i'd create a dictionary then i'd use the second "def gat_vals2" section?
# A dictionary for the different LUC classes and subclasses LUC_dict = { 'estu':'Estuaries', 'ice':'Ice', # Skeleton and Pseudo-code for our function in this exercise def decode_LUC(input_string) #split the input string into two (if needed) #loop through the two strings: #check whether the string consist only of alphabet characters, if so: #look up the string directly #return the decoded string #otherwise: #look up parts of the string seperately #assemble the decoded parts into a lis #return the list
@Ray " So first I'd create a dictionary then I'd use the second "def gat_vals2" section?" Yes, you'd create a dictionary with your values for example, klasses = {key: class, key: class, ...}. It would've been better if you posted the original question here if its available. I personally don't get the whole picture of your question. But speaking about the functions I posted specifically get_vals2 you can pass in any key separated with a delimiter, for example:{1:'a', 2:'b'} if you pass the string "1,2" to the function. It'll give you the values of the keys 1 and 2 respectively.
Though if the key isn't available in the dictionary string 'error' will be returned. Depending on your purpose, you might use function get_vals but again as I mentioned it's not as general as get_vals2.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.