Dies ist die Support Website des Buches:

Das Python Praxisbuch
Der große Profi-Leitfaden für Programmierer
Farid Hajji
Addison Wesley / Pearson Education
ISBN 978-3-8273-2543-3 (Sep 2008), 1298 Seiten.

7. Dictionarys

Dictionary-Literale

Dictionarys abfragen und verändern

Was tun bei nicht-existierenden Einträgen?

Wie verändert man Dictionarys?

Skizze:

Dictionarys mit Default-Initialwerten (setdefault)

Dictionarys durchlaufen

Screenshots:

Dictionarys effizient durchlaufen

Anwendung: word count

Zu indizierende Eingabedatei:

w1 w2 w2 w7 w5 w1 w2 w1 w3 w7 w2 w2 w2 w2 w5 w1
w3 w2 w2 w2 w5 w1 w3 w2 w3 w6 w3 w2 w1 w5 w4 w1
w3 w7 w2 w2 w1 w6 w1 w3 w4 w5 w1 w5 w2 w5 w3 w1
w2 w1 w3 w3 w1 w2 w1 w5 w2 w2 w3 w7 w2 w4 w1 w4

wordcount.dat

mit folgendem Programm:

#!/usr/bin/env python
# wordcount.py -- calculate word counts for all words in a file

def count_words(wordlist):
    '''count_words(wordlist) -> { 'word1': count1, 'word2': count2, ...}

    Count the number of times a word occurs in the word list wordlist.
    Return a dictionary mapping each word to its count.'''

    wordcount = {}
    for word in wordlist:
        wordcount[word] = wordcount.get(word, 0) + 1
    return wordcount

def display_wordcount_by_words(wordcount):
    "Display the word count, sorted by words."

    sorted_by_words = wordcount.keys()
    sorted_by_words.sort()

    outlist = []
    for key in sorted_by_words:
        outlist.append("(%s, %d)" % (key, wordcount[key]))
    print ' '.join(outlist)

def display_wordcount_by_counts(wordcount):
    "Display the word count, sorted by counts."

    # 0. Define a custom comparison function
    def cmp_1st(t1, t2):
        "Compare two tuples, according to their first component"
        return cmp(t1[0], t2[0])

    # 1. sort by words, ascending
    items = wordcount.items()
    items.sort(cmp=cmp_1st)

    # 2. sort by counts, descending (note: sort is stable!)
    backitems = [ (count, word) for word, count in items ]
    backitems.sort(cmp=cmp_1st, reverse=True)

    outlist = []
    for count, word in backitems:
        outlist.append("(%s, %d)" % (word, count))
    print ' '.join(outlist)

def create_word_list(input):
    "Create a list of words read from input string."

    return input.split()

def slurp_data_from_file(filename):
    "Read a text file from filename, return as string."

    # The same as: return open(filename, 'r').read()
    filein = open(filename, 'r')
    file_as_string = filein.read()
    filein.close()

    return file_as_string

if __name__ == '__main__':
    import sys
    if len(sys.argv) < 2:
        print "Usage:", sys.argv[0], "file"
        sys.exit(1)
    filename = sys.argv[1]

    theInputData = slurp_data_from_file(filename)
    theWordList  = create_word_list(theInputData)
    theWordCount = count_words(theWordList)

    print "By words:",
    display_wordcount_by_words(theWordCount)
    print "By count:",
    display_wordcount_by_counts(theWordCount)

wordcount.py

Iteratoren und Generatoren

Was ist ein Iterator?

Einen eigenen Iterator schreiben

Die Klasse TimerIterator:

import time

class TimerIterator(object):
    def __init__(self, interval=1):
        self.interval = interval

    def next(self):
        time.sleep(self.interval)
        return int(time.time())   # Return current time

Die Klasse Timer:

class Timer(object):
    def __init__(self, interval=1):
        self.the_iterator = TimerIterator(interval)

    def __iter__(self):
        return self.the_iterator

Eine Klasse mit integriertem Iterator

Die Klasse Timer2:

class Timer2(object):
    class TimerIterator(object):
        def __init__(self, interval=1):
            self.interval = interval

        def next(self):
            time.sleep(self.interval)
            return int(time.time())   # Return current time

    def __init__(self, interval=1):
        self.the_iterator = Timer2.TimerIterator(interval)

    def __iter__(self):
        return self.the_iterator

Die Klasse Timer3:

class Timer3(object):
    def __init__(self, interval=1):
        self.interval = interval

    def __iter__(self):
        return self

    def next(self):
        time.sleep(self.interval)
        return int(time.time())  # Return current time

Die Klasse Timer4:

class Timer4(object):
    def __init__(self, interval=1):
        self.interval = interval
        self.ticks = -1L

    def __iter__(self):
        return self

    def next(self):
        time.sleep(self.interval)
        self.ticks = self.ticks + 1L
        return self.ticks, int(time.time())

Generatoren

Die Klasse SortedDictIterable:

class SortedDictIterable(object):
    def __init__(self, thedict={}):
        self.thedict = thedict

    def __iter__(self):
        self.thekeys = self.thedict.keys()
        self.thekeys.sort()
        self.index = -1
        return self

    def next(self):
        self.index = self.index + 1
        if self.index >= len(self.thekeys):
            raise StopIteration
        return self.thekeys[self.index]

Es geht einfacher mit Generatoren:

def sorted_dict_generator(aDict):
    thekeys = aDict.keys()
    thekeys.sort()
    for key in thekeys:
        yield key

Generator-Ausdrücke

Die unbegrenzte Funktion urange:

def urange(start=0L):
    "Unlimited range generator"
    index = start
    while True:
        yield index
        index = index + 1L

Zusammenfassung