Python - Loop Woordeboeke
Loop deur 'n woordeboek
Jy kan deur 'n woordeboek loop deur 'n
for
lus te gebruik.
Wanneer u deur 'n woordeboek blaai, is die terugkeerwaarde die sleutels van die woordeboek, maar daar is metodes om die waardes ook terug te gee.
Voorbeeld
Druk alle sleutelname in die woordeboek, een vir een:
for x in thisdict:
print(x)
Voorbeeld
Druk alle waardes in die woordeboek, een vir een:
for x in thisdict:
print(thisdict[x])
Voorbeeld
Jy kan ook die values()
metode gebruik om waardes van 'n woordeboek terug te gee:
for x in thisdict.values():
print(x)
Voorbeeld
Jy kan die keys()
metode gebruik om die sleutels van 'n woordeboek terug te gee:
for x in thisdict.keys():
print(x)
Voorbeeld
Loop deur beide sleutels en waardes deur die
items()
metode te gebruik:
for x, y in thisdict.items():
print(x, y)