Skip to content
Advertisement

Multi-level defaultdict with variable depth?

I have a large list like:

JavaScript

I want to build a multi-level dict like:

JavaScript

I know that if I use recursive defaultdict I can write table[A][B1][C1]=1, table[A][B2]=2, but this works only if I hardcode those insert statement.

While parsing the list, I don’t how many []’s I need beforehand to call table[key1][key2][...].

Advertisement

Answer

You can do it without even defining a class:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement