Python Forum

Full Version: need method to look up dictionary key by value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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.
(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()?
for key, value in dicter.items():