我编写了以下代码来获取给定城市中各个地区的输出及其各自的邮政编码。我希望我的代码能够接收用户(区名)的输入并输出其所在的城市及其邮政编码。如果用户输入邮政编码,他们将获得相应的地区。
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'}
输出:
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'}
>>>
该程序只是转储所有信息,而不是搜索值?
答案 0 :(得分:0)
该程序仅执行两个打印语句print(d_district)
和print(d_code)
,实际上您只是在转储zipcode
字典的内容。永远不会达到您的第一个打印语句,因为永远不会满足该条件。您必须比较值而不是字典的键。
对于相反的情况,您可以检查用户输入的字符串是否为数字,如果是这种情况,则只需以相同的方式搜索邮政编码。
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])
答案 1 :(得分:0)
搜索结果。不懂python。可能仅适用于此示例:尝试:
import pandas as pd
import re
def get_dis_cit():
zpdf =pd.DataFrame(zipcode)#zipdataframe
inpt = input('Enter your zip code or your district: ')
if inpt.isnumeric():
zpdf = zpdf==int(inpt)
district = list(zpdf.columns[zpdf.any()]) + list(zpdf.index[zpdf.T.any()])
return dict(zip(["city","District"], district))
else:
district = re.search('\\b'+inpt+"[^']*",str(zipcode),re.I).group()
city = list(zpdf.loc[district].dropna().index)
return dict(zip(city, [district] * len(city)))
get_dis_cit()
Enter your zip code or your district: 100
Out[71]: {'city': 'Trap City', 'District': 'C District'}
get_dis_cit()
Enter your zip code or your district: 200
Out[72]: {'city': 'Zap City', 'District': 'R District'}
get_dis_cit()
Enter your zip code or your district: r dist
Out[73]: {'city': 'Zap City', 'District': 'R District'}
get_dis_cit()
Enter your zip code or your district: Y DISTRICT
Out[74]: {'city': 'Los City', 'District': 'Y District'}
get_dis_cit()
Enter your zip code or your district: D dist
Out[75]: {'Trap City': 'D District', 'Zap City': 'D District'}
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
}
}