擅长:python、mysql、java
<p>建立一个ID字典,然后检查它们:</p>
<pre><code>>>> a_list=[
... [{'id':'123', 'user':'Foo'}, {'id':'123','user':'Jonny'}, ],
... [{'id':'456', 'user':'Bar'}, {'id':'456','user':'Mary'},],
... ]
>>> b_list=[{'post':'123','text': 'Something'}, {'post':'456', 'text':'Another thing'}, ]
>>> d = {l[0]['id']:l for l in a_list}
>>> for item in b_list:
... item['comments'] = d[item['post']]
...
>>> import pprint
>>> pprint.pprint(b_list)
[{'comments': [{'id': '123', 'user': 'Foo'}, {'id': '123', 'user': 'Jonny'}],
'post': '123',
'text': 'Something'},
{'comments': [{'id': '456', 'user': 'Bar'}, {'id': '456', 'user': 'Mary'}],
'post': '456',
'text': 'Another thing'}]
</code></pre>