Created
February 11, 2014 03:28
-
-
Save stesh/8928821 to your computer and use it in GitHub Desktop.
Instead of defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: defaultdict(float))))
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
#!/usr/bin/python | |
from collections import defaultdict | |
def nested_defaultdict(f, n): | |
if n == 1: | |
return defaultdict(f) | |
else: | |
return defaultdict(lambda: nested_defaultdict(f, n-1)) | |
q = nested_defaultdict(float, 4) | |
print q[1][2][3][4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment