简体   繁体   English

字典/数据库键值检索

[英]Dictionary/Database key value retrieval

As stated in a previous (but different) question, I'm trying to figure out a 'simple' dictionary/database with Python, which will retrieve a name from a list of ten, with the requested information. 如上一个(但又不同)的问题所述,我试图用Python找出一个“简单的”字典/数据库,它将从十个列表中检索一个名称,并包含所请求的信息。 ie input could be 'John phone', where the output is 'John's phone number is 0401' (which I have down pat); 即输入可以是“约翰电话”,其中输出是“约翰的电话号码是0401”(我对此表示不满); but I could also input 'John Birthday' or 'John hobbies' and the output would correspond. 但是我也可以输入“约翰生日”或“约翰爱好”,输出将对应。

As I'm a complete noob, I'm not even sure where to start. 因为我是一个完全菜鸟,所以我什至不知道从哪里开始。 Several hours of googling and poring over lecture notes have yielded nothing so far. 到目前为止,经过数小时的谷歌搜索和研究讲义仍然无济于事。 I've got the feeling it's got something to do with the multiple argument % function but our lecturer really wasn't clear about how to take it further. 我感觉到它与多参数%函数有关,但是我们的讲师确实不清楚如何将其进一步推广。 So far, what I have is: 到目前为止,我所拥有的是:

#!/usr/bin/python

friends = {'John': {'phone' : '0401',
                    'birthday' : '31 July',
                    'address' : 'UK',
                    'interests' : ['a', 'b', 'c']},
           'Harry': {'phone' : '0402',
                    'birthday' : '2 August',
                    'address' : 'Hungary',
                    'interests' : ['d', 'e', 'f']}}
name = raw_input ('Please enter search criteria: ')
if name in friends: 
    print "%s's phone number is: %s" % (name, friends[name]['phone'])
else: 
    print 'no data'

I'd also like to use the 'while' function so the prog doesn't close as soon as that information is given, but not sure if this would be appropriate. 我还想使用“ while”功能,以便该程序不会在给出该信息后立即关闭,但不确定是否合适。 Any pointers would be great, even if it's a 'try this' kind of hint, or a link to a relevant website. 即使是“尝试这种”提示或指向相关网站的链接,任何指针也都很好。

Since this is homework, I'll limit my answer to some hints: 由于这是家庭作业,因此我将回答限于一些提示:

  1. tok = name.split() would split name into a list of words, so 'John address' would become ['John', 'address'] . tok = name.split()会将name分成单词列表,因此'John address'将变为['John', 'address']
  2. You can access the individual words as tok[0] and tok[1] , and use them to index into friends to get the relevant person and then the relevant field. 您可以访问各个单词,如tok[0]tok[1] ,并使用它们将其索引为friends以获取相关人员,然后获取相关字段。
  3. I see no problem with wrapping the input/search logic into a while loop so that the user can perform multiple queries. 我认为将输入/搜索逻辑包装到while循环中没有问题,以便用户可以执行多个查询。 The only thing to figure out is how you're going to exit that loop. 唯一要弄清楚的是您将如何退出该循环。

Based on your sample code, 'John phone' wouldn't work since it would actually be looking up 'John phone' as the name (and 'phone' is hardcoded). 根据您的示例代码,“ John phone”将不起作用,因为它实际上是在查找“ John phone”作为名称(并且“ phone”是硬编码的)。 For illustration, try code like this: 为了说明,请尝试如下代码:

response = raw_input('Please enter search criteria: ').split()

try:
    print "%s's %s is %s" % (response[0], response[1], friends[response[0]][response[1]])
except KeyError:
    print 'no data'

the split() takes each argument separately which can then be referenced with [0] and [1] (and so forth). split()将每个参数分开,然后可以用[0]和[1](依此类推)进行引用。 You were already on the right track with % substitution, but this approach will give it more usability and readability. 使用%替换已经在正确的轨道上,但是这种方法将赋予它更多的可用性和可读性。

The key is that you shouldn't need to check if the name matches (or the criteria matches). 关键是您不需要检查名称是否匹配(或条件匹配)。 Assume it'll be there--and in the event of an exceptional case (that the matches aren't found), it can return 'no data' as expected. 假设它将存在-并且在例外情况下(找不到匹配项),它可以按预期返回“无数据”。

Please enter search criteria: John phone
John's phone is 0401

Please enter search criteria: Harry birthday
Harry's birthday is 2 August

Listen to aix for the first part of your question. 请听aix作为问题的第一部分。 For the second part, I'd suggest coming up with an exit value (perhaps 'exit') that the user could type to leave the program, then have your while loop keep going while input is not that value. 对于第二部分,我建议提出一个退出值(也许是“退出”),用户可以键入该值以退出程序,然后让while循环继续进行,而输入不是该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM