通过用户输入从字典中进行值搜索 [英] Value Search from Dictionary via User Input

查看:65
本文介绍了通过用户输入从字典中进行值搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码来获取给定城市中各个地区的输出及其各自的邮政编码.我希望我的代码能够接收用户(区名)的输入并输出其所在的城市及其邮政编码.如果用户输入邮政编码,他们将获得相应的地区.

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'}

输出:

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?

推荐答案

对结果进行Google搜索.不懂python.可能仅适用于此示例:尝试:

Googled the results. Dont know python. Might be working just for this example: Try:

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
    }
}

这篇关于通过用户输入从字典中进行值搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆