擅长:python、mysql、java
<pre><code>a_list = [
{'ID': 'a', 'Animal': 'dog', 'color': 'white', 'tail': 'yes'},
{'ID': 'a', 'Animal': 'cat', 'color': 'black', 'tail': 'yes'},
{'ID': 'b', 'Animal': 'bird', 'color': 'black', 'tail': 'no'},
{'ID': 'b', 'Animal': 'cat', 'color': 'pink', 'tail': 'yes'},
{'ID': 'b', 'Animal': 'dog', 'color': 'yellow', 'tail': 'no'}
]
a_dict = {}
for v in a_list:
a_dict.setdefault(v['ID'], {}).setdefault(v['Animal'], {}).update(color=v['color'], tail=v['tail'])
from pprint import pprint
pprint(a_dict)
</code></pre>
<p>印刷品:</p>
<pre><code>{'a': {'cat': {'color': 'black', 'tail': 'yes'},
'dog': {'color': 'white', 'tail': 'yes'}},
'b': {'bird': {'color': 'black', 'tail': 'no'},
'cat': {'color': 'pink', 'tail': 'yes'},
'dog': {'color': 'yellow', 'tail': 'no'}}}
</code></pre>