Python Forum
need method to look up dictionary key by value - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: need method to look up dictionary key by value (/thread-7615.html)



need method to look up dictionary key by value - league55 - Jan-17-2018

This is a little class I'm playing around with:

from string import printable
from random import choice

class Anagram:

    def __init__(self, text='', scramble='', key=dict()):
        self.text = text
        self.scramble = scramble
        self.key = key

    def generate_key(self):
        for char in printable:
            self.key[char] = choice(printable)

    def garble(self):
        for char in self.text:
            self.scramble += self.key[char]

    def ungarble(self):
        # need method to look up the key by value in self.key
I've tried to find a method to look up the key by value in order to unscramble the anagram, but haven't found one. Help?


RE: need method to look up dictionary key by value - metulburr - Jan-17-2018

Quote:need method to look up the key by value
loop the key/value pairs and if the value is in that key, print the keys. There could be more than one as values are not unique like keys are.


RE: need method to look up dictionary key by value - league55 - Jan-17-2018

(Jan-17-2018, 10:39 PM)metulburr Wrote: There could be more than one as values are not unique like keys are.

I just noticed that bug. Thanks for pointing it out.

To loop, I should use enumerate()?


RE: need method to look up dictionary key by value - metulburr - Jan-17-2018

for key, value in dicter.items():