Created
May 26, 2016 20:21
-
-
Save arbennett/8768010e3db6d4c29f52f2fe2a50ccb8 to your computer and use it in GitHub Desktop.
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 scdict(dict): | |
""" | |
Separate chaining dictionary implementation allows | |
for multiple values to be assigned to a single key | |
in a list. | |
""" | |
def __setitem__(self, key, val): | |
""" | |
Sets a value to a key, and if the key already exists, | |
make a list of all of the values assigned | |
""" | |
newval = val | |
if key in self: | |
oldval = self.__getitem__(key) | |
if type(oldval) == type([]): | |
newval = oldval + [newval] | |
else: | |
newval = [oldval, val] | |
dict.__setitem__(self, key, newval) | |
def testit(): | |
""" Just show it works """ | |
td = scdict() | |
td['first'] = 'this' | |
td['first'] = 'might' | |
td['first'] = 'work' | |
print(td) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment