Python Forum
need method to look up dictionary key by value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need method to look up dictionary key by value
#1
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?
Reply
#2
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.
Recommended Tutorials:
Reply
#3
(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()?
Reply
#4
for key, value in dicter.items():
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  get method not counting number of strings in dictionary LearningTocode 2 2,098 Jun-13-2020, 11:17 PM
Last Post: LearningTocode
  Pass Dictionary to Method is not possible Frank123456 1 2,352 Aug-19-2019, 10:18 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020