Created
September 26, 2019 03:17
-
-
Save Tarliton/d92ffe6ecfd3a7975f74b0d10074349b 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
from collections import defaultdict | |
create_dict = lambda: defaultdict(create_dict) | |
def auto_defaultidct(keys, d=create_dict()): | |
buffer = '' | |
for i, item in enumerate(range(len(keys))): | |
if i == 0: | |
buffer = 'd' | |
buffer += f'[keys[{item}]]' | |
buffer += ' = create_dict()' | |
exec(buffer, None, locals()) | |
return d | |
auto_defaultidct('this function call will create a defaultdict that expands for every word in this sentece'.split(' ')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment