Update dictionary keys to be in ascending order?
<p>I have a dictionary:</p>

<pre><code>A = {'cat': {0: {'variable_1': 'xxx', 'variable_2': 'yyy'},
             1: {'variable_1': 'ttt', 'variable_2': 'kkk'}},
     'dog': {0: {'variable_1': 'xxx', 'variable_2': 'ppp'},
             1: {'variable_1': 'qqq', 'variable_2': 'www'}},
     'fox': {0: {'variable_1': 'xxx', 'variable_2': 'zzz'},
             1: {'variable_1': 'yyy', 'variable_2': 'uuu'},
             3: {'variable_1': 'ccc', 'variable_2': 'jjj'}}}
</code></pre>

<p>I want to make the second level key automated ascend
like this:</p>

<pre><code>{'cat': {0: {'variable_1': 'xxx', 'variable_2': 'yyy'},
         1: {'variable_1': 'ttt', 'variable_2': 'kkk'}},
 'dog': {2: {'variable_1': 'xxx', 'variable_2': 'ppp'},
         3: {'variable_1': 'qqq', 'variable_2': 'www'}},
 'fox': {4: {'variable_1': 'xxx', 'variable_2': 'zzz'},
         5: {'variable_1': 'yyy', 'variable_2': 'uuu'},
         6: {'variable_1': 'ccc', 'variable_2': 'jjj'}}}
</code></pre>

<p>if u don't need to keep the orderï¼u can copy the dict to other dict. A global variable can keep the second level key.</p>

<pre><code># coding=utf-8

A={'cat':{0:{'variable_1':'xxx','variable_2':'yyy'},1:{'variable_1':'ttt','variable_2':'kkk'}},
    'dog':{0:{'variable_1':'xxx','variable_2':'ppp'},1:{'variable_1':'qqq','variable_2':'www'}},
  'fox':{0:{'variable_1':'xxx','variable_2':'zzz'},1:{'variable_1':'yyy','variable_2':'uuu'},3:{'variable_1':'ccc','variable_2':'jjj'}}}

B = dict()
index = 0
for key in A:
    animal = A[key]
    B[key] = dict()
    for i in animal:
        B[key][index] = animal[i]
        index += 1
print(B)
</code></pre>

<p>output:</p>

<pre><code>{
'dog': {
 0: {'variable_1': 'xxx', 'variable_2': 'ppp'}, 
 1: {'variable_1': 'qqq', 'variable_2': 'www'}
}, 
'fox': {
 2: {'variable_1': 'xxx', 'variable_2': 'zzz'}, 
 3: {'variable_1': 'yyy', 'variable_2': 'uuu'}, 
 4: {'variable_1': 'ccc', 'variable_2': 'jjj'}
}, 
'cat': {
 5: {'variable_1': 'xxx', 'variable_2': 'yyy'}, 
 6: {'variable_1': 'ttt', 'variable_2': 'kkk'}
}
}
</code></pre>

python dictionary key value auto-update