ich habe Probleme den letzten Eintrag eines Dict`s in einem Dict zu bekommen.
Code: Alles auswählen
dictionary = {0: {1: 'hallo'}, 1: {5: 'moin', 10: 'hallo'}, 2: {15: 'moin'}}
print(list(dictionary.keys())[-1])
Kann mir da bitte jemand helfen?
Code: Alles auswählen
dictionary = {0: {1: 'hallo'}, 1: {5: 'moin', 10: 'hallo'}, 2: {15: 'moin'}}
print(list(dictionary.keys())[-1])
Code: Alles auswählen
key = list(dictionary.items())[-1]
values = list(dictionary.items())[-1][-1]
print(key)
print(values)
Code: Alles auswählen
dictionary = {0: {1: 'hallo'}, 1: {5: 'moin', 10: 'hallo'}, 2: {15: 'moin', 16: 'tag'}}
print(list(dictionary.items())[-1])
Der Schlüssel "2" des Dicts `dictionary` ist wiederum ein Dict und das hat wiederum zwei Werte.key = 2, key_value=16, text='tag'
Code: Alles auswählen
>>> d = {0: {1: 'hallo'}, 1: {5: 'moin', 10: 'hallo'}, 2: {15: 'moin', 16: 'tag'
}}
>>> last_outer_key, last_outer_value = list(d.items())[-1]
>>> last_inner_key, last_inner_value, = list(last_outer_value.items())[-1]
>>> print(f'{last_outer_key} - {last_inner_key} - {last_inner_value}')
2 - 16 - tag
>>>