Value Search from Dictionary via User Input

2024/10/14 9:24:03

I have written the following code for getting an output of the various districts located in the given city and their respective postal codes. I want my code to be able to receive input from the user (District Name) and output the City in which it is located and it's postal code. If the user inputs a postal code they'll get the respective district(s).

zipcode = {"Trap City":{"C District": 100, "D District": 103, "E District": 104, "S District": 105}, "Zap City":{"R District": 200, "D District": 201},"Los City": {"X District": 207, "Y District": 208}}district=input('Enter your district: ')for city in zipcode:if district in city:print(city,zipcode[city][district])d_district = {k2: (k1, v2) for k1, v1 in zipcode.items() for k2, v2 in 
v1.items()}print(d_district){'C District': ('Trap City', 100),
'D District': ('Zap City', 201),
'E District': ('Trap City', 104),
'R District': ('Zap City', 200),
'S District': ('Trap City', 105),
'X District': ('Los City', 207),
'Y District': ('Los City', 208)}d_code = {v2: k2 for k1, v1 in zipcode.items() for k2, v2 in 
v1.items()}print(d_code){100: 'C District',
103: 'D District',
104: 'E District',
105: 'S District',
200: 'R District',
201: 'D District',
207: 'X District',
208: 'Y District'}

Output:

Enter your district: X District
{'C District': ('Trap City', 100), 'D District': ('Zap City', 201), 'E 
District': ('Trap City', 104), 'S District': ('Trap City', 105), 'R 
District': ('Zap City', 200), 'X District': ('Los City', 207), 'Y 
District': ('Los City', 208)}
{100: 'C District', 103: 'D District', 104: 'E District', 105: 'S 
District', 200: 'R District', 201: 'D District', 207: 'X District', 
208: 'Y District'}
>>> 

The program instead just dumps all the information instead of searching for the value?

Answer

The program does only execute the two print statements print(d_district) and print(d_code), where you actually just dump the contents of the zipcode dictionary. Your first print statement is never reached, as the condition never gets fulfilled. You have to compare to the value and not the key of the dictionary.

For the reversed case, you can check if the string entered by the user is numeric, and if this is the case, you just search for the zipcode in the same manner.

mydistrict=input('Enter your district: ')if mydistrict.strip().isnumeric():for city in zipcode:for district in zipcode[city]:if zipcode[city][district] == int(mydistrict.strip()):print(city,zipcode[city][district])
else:for city in zipcode:if mydistrict in zipcode[city]:print(city,zipcode[city][mydistrict])
https://en.xdnf.cn/q/117973.html

Related Q&A

Read and aggregate data from CSV file

I have a data file with the following format:name,cost1,cost1,cost1,cost2,cost3,cost3, X,2,4,6,5,6,8, Y,0,3,6,5,4,6, . . ....Now, what I would like to do is to convert this to a dictionary of dictionar…

nltk cant using ImportError: cannot import name compat

This is my codeimport nltk freq_dist = nltk.FreqDist(words) print freq_dist.keys()[:50] # 50 most frequent tokens print freq_dist.keys()[-50:] # 50 least frequent tokensAnd I am getting this error mess…

Fitting and Plotting Lognormal

Im having trouble doing something as relatively simple as:Draw N samples from a gaussian with some mean and variance Take logs to those N samples Fit a lognormal (using stats.lognorm.fit) Spit out a n…

Is there any way to install nose in Maya?

Im using Autodesk Maya 2008 on Linux at home, and Maya 2012 on Windows 7 at work. Most of my efforts so far have been focused on the former. I found this thread, and managed to get the setup there work…

Basic python socket server application doesnt result in expected output

Im trying to write a basic server / client application in python, where the clients sends the numbers 1-15 to the server, and the server prints it on the server side console. Code for client:import soc…

creating dictionaries to list order of ranking

I have a list of people and who controls who but I need to combine them all and form several sentences to compute which person control a list of people.The employee order comes from a txt file:

Python: How to use MFdataset in netCDF4

I am trying to read multiple NetCDF files and my code returns the error:ValueError: MFNetCDF4 only works with NETCDF3_* and NETCDF4_CLASSIC formatted files, not NETCDF4. I looked up the documentation a…

Pyspark: Concat function generated columns into new dataframe

I have a pyspark dataframe (df) with n cols, I would like to generate another df of n cols, where each column records the percentage difference b/w consecutive rows in the corresponding, original df co…

Mysql.connector to access remote database in local network Python 3

I used mysql.connector python library to make changes to my local SQL server databases using: from __future__ import print_function import mysql.connector as kkcnx = kk.connect(user=root, password=pass…

concurrent.futures not parallelizing write

I have a list dataframe_chunk which contains chunks of a very large pandas dataframe.I would like to write every single chunk into a different csv, and to do so in parallel. However, I see the files be…