Dynamic Lists, or...?

Raymond Hettinger python at rcn.com
Mon Jun 13 04:04:53 EDT 2005


# Professional driver on a closed course.
# Don't try this at home.

data = """
rose, 1, 500
lilac, 1, 300
lilly, 1, 400
rose, 0, 100
"""

data = data.replace(', 1, ', ' += ')
data = data.replace(', 0, ', ' -= ')

class DefaultDict(dict):
    def __getitem__(self, key):
        return self.get(key, 0)

d = DefaultDict()
exec data in {}, d
print d.items()




More information about the Python-list mailing list