Suporno Chaudhury
Suporno Chaudhury

Reputation: 67

Retrieving value when partial key is given in dictionary

I have a question in Python where we have input of first name of the employee, last name of the employee and his/her phone number.

We have to make a directory where the number of all the employee having common name should be generated if we give either the first name, last name, or both.

E.g. Given name of the employee as John the program must print phone numbers of John Paul and Michel John.

d = {}
a = int(input('the total number of employees in the company:'))

for i in range(0, a):
    x = input('first name of the employee:')
    y = input('the last name of the employee:')
    z = int(input('the phone number of the employee:'))
    d2 = {x: z, y: z}
    d.update(d2)

b = input('the partial or the full name of the employee you need the phone number of:')
print(d[b])

I could find the error in my code (when the dictionary is updated in case of common name instead of creating a new entry hence only one number (last entry) is displayed in case of common entry).

Upvotes: 3

Views: 480

Answers (3)

eph
eph

Reputation: 2028

If you want to gain speed with dict, you can use two dicts to perform this task. The first dict is a mapping from full name to phone number, and the second one is a mapping from common name to a list of full names.

>>> phones = {'John Paul': '12345', 'Michael John': '54321'}
>>> names = {'John': ['John Paul', 'Michael John'],
...          'Paul': ['John Paul'],
...          'Michael': ['Michael John']}
>>> def get_phones(common_name):
...     for full_name in names[common_name]:
...         yield full_name, phones[full_name]
...
>>> list(get_phones('John'))
[('John Paul', '12345'), ('Michael John', '54321')]

The insertion of new recode is similar to this:

def insert_phone(full_name, phone):
    for common_name in full_name.split():
        if common_name not in names: names[common_name] = []
        names[common_name].append(full_name)
    phones[full_name] = phone

Upvotes: 1

RomaEcho
RomaEcho

Reputation: 1

I reworked example a little:

d = {}
st = ''
a = int(input('the total number of employees in the company:'))

for i in range(0, a):
    d[i]['firstname'] = input('first name of the employee:')
    d[i]['lastname'] = input('the last name of the employee:')
    d[i]['phone'] = input('the phone number of the employee:')

b = input('the partial or the full name of the employee you need the phone number of:')
for k, v in d.items():
    for k1, v1 in v.items():
        if b in v1:
            st += ' '.join([v['firstname'], v['lastname'], v['phone'], '\n'])
print(st)

>>> d[0] = {'firstname': 'alex', 'lastname': 'brooke', 'phone': '22234'}
>>> d[1] = {'firstname': 'kelvin', 'lastname': 'mario', 'phone': '22333'}
>>> d[2] = {'firstname': 'kelvin', 'lastname': 'santa', 'phone': '233989'}
>>> b = 'kelvin'
>>> print(st)
kelvin mario 22333
kelvin santa 233989

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174786

Just iterate over the dict keys and get the corresponding value of a key where the given input resides in.

print([d[i] for i in d if b in i]) 

Example:

>>> d = {'John paul': 'foo', 'Michael John': 'bar'}

>>> b = 'John'
>>> [d[i] for i in d if b in i]
['bar', 'foo']

>>> b = 'jOhN'
>>> [d[i] for i in d if b.lower() in i.lower()]
['bar', 'foo']

Upvotes: 3

Related Questions