0

lets say I have 2 strings that are interchangeable, like a full word and it's abbreviation: 'max' and 'maximum'

I would like to set it so that they respond the same, for example if i have the following dictionary:

d = {'max':10,'a':5,'b':9}

d['maximum'] will return 10

is this even remotely possible?

note:

these two strings could be 'dog' and 'cat', they do not have to be related.

what I am asking is if I could do something like:

a = 'a' or 'b'

that way the two strings are interchangeable. I do understand that above is not correct syntax, I am just curious if anything like it is possible

7
  • 1
    I am not asking specifically as a dictionary. I am asking if it is possible to have any time one string is referenced in my code to have it work as if either string is referenced...a dictionary was a good example and that is why I used it Commented Aug 15, 2013 at 6:50
  • 1
    the abbreviation was also just for a real world example Commented Aug 15, 2013 at 6:50
  • I think you need to qualify this a little better ... interchangable in what sense? If you want them to be interchangeable in a dictionary, the answer is no. "max" and "maximum" aren't the same string. You could subclass dict and make sure to only use one or the other ... but that only catches the dictionary case (and it doesn't work with literals).... Commented Aug 15, 2013 at 6:54
  • @mgilson: exactly my question though, I am asking if I could have a variable act as multiple strings, where it would go down the line somewhat like nested try and excepts Commented Aug 15, 2013 at 6:58
  • Dict if the best way to do this({'a':'b', 'dog':'cat'}), if you want two way mapping use bidict. Commented Aug 15, 2013 at 6:59

2 Answers 2

5

You can do that using two dicts:

>>> key_dic = {'maximum':'max', 'minimum':'min'}
>>> d = {'max':10,'a':5,'b':9, 'min':-1}
def get_value(key):
    return d[key_dic.get(key, key)]
... 
>>> get_value('maximum')
10
>>> get_value('max')
10
>>> get_value('min')
-1
>>> get_value('minimum')
-1
Sign up to request clarification or add additional context in comments.

2 Comments

please read my edited question...the dictionary was simply an example...the real question is if I can somehow assign a variable to respond to more than one string
@RyanSaxe For variables the short answer is NO, you can't make a variable point to two different objects at a time.
0

You'll need to convert it into a function, class or something similar.

d_array = {'max':10,'a':5,'b':9}

def d(keyword):
    if keyword == "maximum":
        keyword = "max"
    return d_array[keyword]


>>>print d("maximum")
10

1 Comment

please read my edited question...the dictionary was simply an example...the real question is if I can somehow assign a variable to respond to more than one string

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.