How to implement Dictionary with Python3? - GeeksforGeeks

04 Dec, 2019
This program uses python’s container called dictionary (in dictionary a key is associated with some information). This program will take a word as input and returns the meaning of that word.Python3 should be installed in your system. If it not installed, install it from this link. Always try to install the latest version.
I made a text file in which word and its meaning is stored in python’s dictionary formatExample :
data = {"geek" : "engage in or discuss computer-related tasks 
obsessively or with great attention to technical detail."}

Here if we call “geek” from data then this will return its meaning “engage in or discuss computer-related tasks obsessively or with great attention to technical detail.”.
This python program allow you to fetch the data of this text file and give the meaning.
# Python3 Code for implementing# dictionary  # importing json libraryimport json  # importing get_close_matches function from difflib libraryfrom difflib import get_close_matches  # loading datadata = json.load(open("data.txt"))  # defining function meaningdef meaning(w):      # converting all the letters of "w" to lower case    w = w.lower()      # checking if "w" is in data    if w in data:        return data[w]      # if word is not in data then get close match of the word    elif len(get_close_matches(w, data.keys())) > 0:          # asking user for his feedback        # get_close_matches returns a list of the best         # “good enough” matches choosing first close         # match "get_close_matches(w, data.keys())[0]"        yn = input("Did you mean % s instead? Enter Y if yes, or N if no:        " % get_close_matches(w, data.keys())[0])                  if yn == "Y":            return data[get_close_matches(w, data.keys())[0]]        elif yn == "N":            return "The word doesn't exist in our data."        else:            return "We didn't understand your entry."    else:        return "The word doesn't exist in our data."  # asking word from user to get the meaningword = input("Enter word: ")  # storing return value in "output"output = meaning(word)  # if output type is list then print all element of the listif type(output) == list:    for item in output:        print(item)  # if output type is not "list" then print output onlyelse:    print(output)
How to run?
Download this data file and save it in the same folder where your python code file is saved.Make sure that both the file(data file and the code file) are in same folder.Open command prompt in that folder to do so press shift then right click in mouse.Run the python code using cmd(command prompt).Input the word whose meaning is to be searched.Output will be your result. Video DemonstrationVideo Playerhttps://media.geeksforgeeks.org/wp-content/uploads/ujjuDictionary-1.mp400:0000:0003:11Use Up/Down Arrow keys to increase or decrease volume.My Personal Notes
arrow_drop_upSave
Download this data file and save it in the same folder where your python code file is saved.
Make sure that both the file(data file and the code file) are in same folder.
Open command prompt in that folder to do so press shift then right click in mouse.
Run the python code using cmd(command prompt).
Input the word whose meaning is to be searched.
Output will be your result. Video DemonstrationVideo Playerhttps://media.geeksforgeeks.org/wp-content/uploads/ujjuDictionary-1.mp400:0000:0003:11Use Up/Down Arrow keys to increase or decrease volume.My Personal Notes
arrow_drop_upSave
 
Video Demonstration
Akanksha_Rai
python-string
GBlog
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Top 10 Front End Developer Skills That You Need in 2022
DSA Sheet by Love Babbar
6 Best IDE's For Python in 2022
A Freshers Guide To Programming
ML | Underfitting and Overfitting
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe