d = DictoList()
d.add_keys("a", "b")
DictoList:
a
b
d.add_keys("a", "b", "x1")
DictoList:
a
b
x1
d.add_keys("a", "b", "x2")
DictoList:
a
b
x2
x1
d.add_keys("a", "c")
DictoList:
a
c
b
x2
x1
d.get("a")
['c', 'b']
d.get("a", "b")
['x2', 'x1']
d.get("a", "?")
[]
Created
July 29, 2012 17:33
-
-
Save odwrotnie/3200447 to your computer and use it in GitHub Desktop.
DictoList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DictoList: | |
def __init__(self): | |
self.d = {} | |
def add_keys(self, *keys): | |
d = self.d | |
for k in keys: | |
if not k in d: | |
d[k] = {} | |
d = d[k] | |
def get(self, *keys): | |
d = self.d | |
for k in keys: | |
if not k in d: | |
return [] | |
d = d[k] | |
return d.keys() | |
def level(self, d, level): | |
s = "" | |
for key in d.keys(): | |
s = s + "\n" + " " * level + key + self.level(d[key], level + 1) | |
return s | |
def __str__(self): | |
return "DictoList:" + self.level(self.d, 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment