-3

For example, I have this:

alphabetValues = {"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7...

Is it possible if instead of having:

print(alphabetValues["c"])

To having something that would get "e" if I searched for 5 in a dict.

"e":5

Thanks in advance.

2 Answers2

0

Why not set up an alphabet list?

alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

print(alphabet[0]) #will print out 'a'
print(alphabet[25]) #will print out 'z'

Note that all values are 1 less than expected.

Constantly Confused
  • 595
  • 4
  • 10
  • 24
0

As suggested by jonrsharpe, you need to reverse your dictionnary :

alphabetValues = {"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7}

revalpha={v:k for k,v in alphabetValues.iteritems()}

>>> revalpha[5]
'e'
manu190466
  • 1,557
  • 1
  • 9
  • 17
  • This works great, thanks for the help, I'm only just getting to grips with Python. – hermitspike Dec 07 '15 at 22:44
  • This didn't work for me straight away, I've used revalpha={v:k for k,v in alphabetValues.items()} Instead. – hermitspike Dec 08 '15 at 17:33
  • I guess you're running python3. The method iteritems has been removed in version 3 and we now have to use item instead (cf. http://stackoverflow.com/questions/13998492/iteritems-in-python) – manu190466 Dec 08 '15 at 20:24