python 搜索字典值并打印

标签 python python-3.x dictionary jupyter-notebook

dictionary = {'Name':['Sam','Rafael','Albert','Prapul','Zorg','Peter','Sandy','Kristena','Noah','William','Alexander'],
              'Number':[9842657266,2548759249,5859715540,9874515875,8974511147,9874447574,5987415877,8898874714,9985852124,
                        8015005998,9633215749],
              'Email':['sam@gmail.com','raf@outlook.com','albert12@gmail.com','prapul@yahoo.com','zorg124@hotmail.com',
                       'pete345@yahoo.com','sandy007@outlook.com','kristena789@hotmail.com','noah123do@gmail.com',
                       'william12sam@gmail.com','alex65tgp@hotmail.com']}


 search = input('Please enter the name you would like to search:')
        if search in dictionary.keys():
            #print user details
        else:
            print ('User Not Found')

我需要在字典中搜索特定用户并打印用户名电话号码和电子邮件地址

最佳答案

您可以通过以下方式在词典中搜索特定用户并打印用户名电话号码和电子邮件地址:

dictionary = {'Name': ['Sam','Rafael','Albert','Prapul','Zorg','Peter','Sandy','Kristena','Noah','William','Alexander'],
              'Number':[9842657266,2548759249,5859715540,9874515875,8974511147,9874447574,5987415877,8898874714,9985852124,
                    8015005998,9633215749],
              'Email':['sam@gmail.com','raf@outlook.com','albert12@gmail.com','prapul@yahoo.com','zorg124@hotmail.com',
                   'pete345@yahoo.com','sandy007@outlook.com','kristena789@hotmail.com','noah123do@gmail.com',
                   'william12sam@gmail.com','alex65tgp@hotmail.com']}

search = input('Please enter the name you would like to search:\n')
index = -1
for i in range(len(dictionary['Name'])):
    if dictionary['Name'][i] == search:
        index = i

if index >= 0:
    print('Phone number:', dictionary['Number'][index])
    print('Email:', dictionary['Email'][index])
else:
    print('User Not Found')

结果

>>> Please enter the name you would like to search:
>>> Albert
>>> Phone number: 5859715540
>>> Email: albert12@gmail.com

关于python 搜索字典值并打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53789696/

相关文章:

python - Pandas - 将直方图桶分配给每一行

python - 从初始化程序运行实例方法时出现神秘的 "extra argument"错误

Python-合并两个字典列表,添加重复键的值

python - 安装Python模块六

python - 比较字符串中的前几个字符

python - 使用 python 以 CSS 方式编码字符串

python - 从二进制文件中解码 python 中的字符串列表

python - 如何在请求中定义 HTTP 协议(protocol)版本?

python - 在Python中解压字典并获取值

android - 谷歌地图 API v2。安卓