Oct-08-2023, 06:38 AM
My Ergebnis is a list of dictionaries.
Assuming there is a result, you get the list Ergebnis, for a name like Swensonl. Each element in the list is dictionary.
You could do that like this:
Assuming there is a result, you get the list Ergebnis, for a name like Swensonl. Each element in the list is dictionary.
Ergebnis = findName(person) if not Ergebnis == 'Nix da!': # Ergebnis is a list, walk through it for E in Ergebnis: print(E)I get this output when I enter the surname Swenson:
Output:{'vorname': 'Thom', 'name': 'Swenson', 'adress': 'bethastr. 37'}
{'vorname': 'Dumb', 'name': 'Swenson', 'address': 'Gottlobstr. 99'}
You could do that like this:
# Ergebnis is a list of dictionaries if not Ergebnis == 'Nix da!': # E is a dictionary for E in Ergebnis: for key in E.keys(): print('key is', key, 'value is', E[key])Your searchName(telefonbook, name) function works but returns a dictionary of dictionaries, if the surname is present more than 1 time, as in my telefonbook.
def searchName(telefonbook, name): """Durchsucht ein Adressbuch nach einem Namen.""" results = {} # item is a tuple of (key, value) for item in telefonbook.items(): person = item[1] if name in person.values(): results[item[0]] = person return resultsSo then do:
Resultat = searchName(telefonbook, name) for key in Resultat.keys(): print('key is', key, 'data is', Resultat[key])Wer suchet, der findet!!