Python Forum
to find in dictionary given parameter 'name' and to output position
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
to find in dictionary given parameter 'name' and to output position
#1
Hello!

Given ist the dictionary "telefonbook" with telefon-Nr. as the key, and with vorname, name, adress as the parameters to the keys.

I'm trying to write a programm which with the funktion "searchName" with an given name also all rest parameter printed.

What I doing wrong?:

telefonbook = {
    '52307': {'vorname': 'Max', 'name': 'Mustermann', 'adress': 'poststr. 15'},
    '25758': {'vorname': 'Thom','name': 'Swenson', 'adress': 'bethastr. 37'},
    '01579': {'vorname': 'Jeff', 'name': 'Peters', 'adress': 'sportstr. 584'}
    }

def searchName(telefonbook, name):
     """Durchsucht ein Adressbuch nach einem Namen.
  
     Durchsucht die Dictionary-Objekte eines Adressbuchs, ob ein Name 
     in deren Werten vorkommt.
  
     Arguments:
         telefonbook (dict): Das Adressbuch, welches durchsucht werden soll
         name (str): Der Name, nach dem gesucht wird
  
     Returns:
         dict: Ein Teiladressbuch mit den gefundenen Adressen und vornamen
     """

     results = {}
     for item in telefonbook.items():
         person = item[1]
         if name in person.values():
             results[item[0]] = person
     return results

ergebnis = searchName(telefonbook, 'Swenson')
print(ergebnis)
Reply


Messages In This Thread
to find in dictionary given parameter 'name' and to output position - by Liki - Oct-03-2023, 01:26 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  dictionary output to text file (beginner) Delg_Dankil 2 1,318 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  Using dictionary to find the most sent emails from a file siliusu 6 7,754 Apr-22-2021, 06:07 PM
Last Post: siliusu
  From string parameter to a dictionary Mitchie87 9 3,311 Oct-12-2019, 10:34 PM
Last Post: Larz60+
  how to find 'cycle' for key-value pairs in a dictionary? junnyfromthehood 1 3,665 Sep-29-2019, 01:07 AM
Last Post: ichabod801
  Powerball assignment, how to get correct output of a dictionary ? msp1981 5 3,399 Mar-19-2019, 11:02 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020