Python поиск значений словаря и печать - PullRequest
0 голосов
/ 15 декабря 2018
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')

Мне нужно найти конкретного пользователя в словаре и распечатать имя пользователя, номер телефона и адрес электронной почты

Ответы [ 4 ]

0 голосов
/ 15 декабря 2018

С небольшими изменениями в коде Стивена:

def search(keyword):
    for x in zip(*(dictionary[key] for key in (dictionary.keys()))):
        if x[0] == keyword:
            return x[1],x[2]
0 голосов
/ 15 декабря 2018

Вы можете найти конкретного пользователя в словаре и распечатать имя пользователя, номер телефона и адрес электронной почты следующим образом:

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
0 голосов
/ 15 декабря 2018

Ваш вопрос решен, но у меня есть другая логика:

dictionary = {'Sam':[9842657266,'sam@gmail.com'],'alex':[2548759249,'william12sam@gmail.com']}

search = input('Please enter the name you would like to search : ')

if search in dictionary.keys():
    #here your condition
    print('Phone Number :-> ' , dictionary[search][0] ) #index 0 is Phone number
    print('Email :-> ' , dictionary[search][1]) #index 1 is email
else :
    print('User Not Found')
0 голосов
/ 15 декабря 2018

Функция, подобная:

Код:

def find_name(name_to_find):
    return next((x for x in zip(*(
        dictionary[key] for key in ('Name', 'Number', 'Email')))
                 if x[0] == name_to_find), None)

сделает это.

Код теста:

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

print(find_name('Zorg'))
print(find_name('Junk'))

Результаты:

('Zorg', 8974511147, 'zorg124@hotmail.com')
None
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...