在用户输入中搜索关键字,并在字典中找到相应的关键字



我正在做一个搜索引擎,我试图编码,以便当用户输入一些关键字时,它会在字典中搜索关键字并打印密钥和项目。

Canada = {'name': 'Canada', 'continent': 'North America', 'capital': 'Ottawa', 'currency': 'Canadian dollar',
'population': '32,268,240', 'area': '9,970,610'}
Laos = {'name': 'Laos', 'continent': 'Asia', 'capital': 'Vientiane', 'currency': 'Lao kip',
'population': '5,924,145', 'area': '236,800'}
Mexico = {'name': 'Mexico', 'continent': 'North America', 'capital': 'Mexico City', 'currency': 'Mexico peso',
'population': '107,029,400', 'area': '1,958,201'}
key_words = ("Canada", "Mexico", "Laos")
key = ("area", "population", "currency")
user_input = input("Type: ")
for word in user_input.split():
if word in key_words:
print()

你能帮我编码吗,以便: 例如: 当用户输入:"墨西哥货币"时,它将打印出:墨西哥比索,或者当用户输入:"老挝地区"时,它将打印出236,800。

您需要为用户设置一些规则,您可以从头开始构建模糊查询搜索引擎,但这不是今天要发生的事情。现在,让用户指定他们想要的内容,例如。ITEM COUNTRY.

Enter <query> <country>: currency mexico
>> Mexico pesos

您可以像使用一个字典来保存所有数据而不是将其分解为变量一样重组代码。

database = {
'canada': {
'name': 'Canada',
'continent': 'North America',
'capital': 'Ottawa',
'currency': 'Canadian dollar',
'population': '32,268,240',
'area': '9,970,610'
},
'laos' : {
'name': 'Laos',
'continent': 'Asia',
'capital': 'Vientiane',
'currency': 'Lao kip',
'population': '5,924,145',
'area': '236,800'
},
'mexico': {
'name': 'Mexico',
'continent': 'North America',
'capital': 'Mexico City',
'currency': 'Mexico peso',
'population': '107,029,400',
'area': '1,958,201'
}
}
user_input = input("Enter your query: ")
user_input = user_input.lower().split(' ')
# find country
country = None
for keyword in user_input:
if keyword in database:
# using .pop() removes the country from the list
country = user_input.pop(user_input.index(keyword))
# If the user entered a valid country
if country:
for keyword in user_input:
if keyword in database.get(country):
print(keyword, database[country][keyword])
else:
print('Could not find that country in the database')

编辑:更新了代码,因此您可以更加模棱两可,并以任何顺序键入关键字/国家/地区,并具有多个关键字

例:

Enter query: population and currency of mexico
>> currency Mexico peso
>> population 107,029,400

你可以试试:

Canada = {'name': 'Canada', 'continent': 'North America', 'capital': 'Ottawa', 'currency': 'Canadian dollar',
'population': '32,268,240', 'area': '9,970,610'}
Laos = {'name': 'Laos', 'continent': 'Asia', 'capital': 'Vientiane', 'currency': 'Lao kip',
'population': '5,924,145', 'area': '236,800'}
Mexico = {'name': 'Mexico', 'continent': 'North America', 'capital': 'Mexico City', 'currency': 'Mexico peso',
'population': '107,029,400', 'area': '1,958,201'}
countries = {'Canada' : Canada, 'Mexico' :Mexico, 'Laos' : Laos} 
user_input = input("Type: ")
asked_country = list(set(user_input.split()).intersection(countries.keys()))[0] 
output = list(set(countries[asked_country].keys()))[0]
print(countries[asked_country][output]) 
# Type: currency of Mexico             
# Mexico peso

不短,但更清晰、更宽广的解决方案。例如,对于像Area and population of Canada这样的查询,它将打印:

Type: area and population of Canada
results:
area = 9,970,610
population = 32,268,240
from collections import defaultdict
Canada = {'name': 'Canada', 'continent': 'North America', 'capital': 'Ottawa', 'currency': 'Canadian dollar',
'population': '32,268,240', 'area': '9,970,610'}
Laos = {'name': 'Laos', 'continent': 'Asia', 'capital': 'Vientiane', 'currency': 'Lao kip',
'population': '5,924,145', 'area': '236,800'}
Mexico = {'name': 'Mexico', 'continent': 'North America', 'capital': 'Mexico City', 'currency': 'Mexico peso',
'population': '107,029,400', 'area': '1,958,201'}
key = set(["area", "population", "currency"])
# build the forward and reverse indexes
forward_index = [Canada, Laos, Mexico]
reverse_index = defaultdict(set)
for i, description in enumerate(forward_index):
phrase = ' '.join(['%s %s'%(k,v) for k, v in description.items()])
words = phrase.lower().split()
for w in words:
reverse_index[w].add(i)
# search
user_input = input("Type: ")
res = None
keywords = set()
for word in user_input.lower().split():
if word in key:
keywords.add(word)
posting_set = reverse_index.get(word, None)
# tolerant search: skip missing words
if posting_set is None:
continue
if res is None:
res = posting_set
else:
res = res.intersection(posting_set)
# print all the results
if res is None or len(keywords) == 0:
print("<nothing found>")
else:
print("results:")
for idx in res:
desc = forward_index[idx]
for kw in keywords:
print("%s = %s" % (kw, desc[kw]))

相关内容

  • 没有找到相关文章

最新更新