Enhancing this question, I got:
def pretty(d, steps = -1, indent = 0):
for key, value in d.iteritems():
c = 0
print '\t' * indent + str(key)
if isinstance(value, dict):
pretty(value, indent+1)
else:
print c
if(c == steps):
return
c += 1
print '\t' * (indent+1) + str(value)
which dreams of printing until a certain number of values of the original key are printed. For example, without a limit:
1
5299
1
1229
1
2068
1
7223
1
but when steps = 2
, then it would print:
1
5299
1
1229
1
The reason its dreams are not coming true is that c
is not a static variable (like one would do in C, I tried static c = 0
and got a syntax error), thus at every call of the recursion, c
is created again, it doesn't remember its former value.
How would one do this in Python?