Iteratoren

Iteratoren in Python

Variante 1 - iterkeys()

Viel Schreibarbeit, wenig „Gewinn“ an Übersicht. Iterator liefert nicht den Wert.

dictionary = {'1': 'one', '2': 'two', '3': 'there'}
 
for key in dictionary.iterkeys():
    print key, dictionary[key]

Variante 2 - keys()

Wie Variante 1

for key in dictionary.keys():
    print key, dictionary[key]

Variante 3 - items()

Gut weil: kein direkter Zugriff aufs dict, wenig Schreibarbeit

for key, value in dictionary.items():
    print key, value
public/python/iterators.txt · Zuletzt geändert: 2009/04/24 20:39 (Externe Bearbeitung)
chimeric.de = chi`s home Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0