擅长:python、mysql、java
<p>看看这个脚本:</p>
<pre><code>>>> MaleDict = {'Jason':[(2014, 394),(2013, 350)], 'Stephanie':[(2014, 3), (2013, 21),]}
>>> FemaleDict = {'Jason':[(2014, 56),(2013, 23)], 'Stephanie':[(2014, 335), (2013, 217),]}
>>>
>>> #clone MaleDict, to have a base to append FemaleItems
... combined_dict = dict(MaleDict)
>>> for name, items in FemaleDict.items():
... for idx,year_tuple in enumerate(combined_dict.get(name,[])):
... year_tuple = list(year_tuple) #convert to list to let us modify its items
... for year_data in items[idx]: #inspect same year-element of both dicts
... if year_data not in year_tuple: # if the current part doesn't exist, append it to temp list
... year_tuple.append(year_data)
... combined_dict[name] [idx] = tuple(year_tuple) #finally replace the current tuple by new one
...
>>>
>>> combined_dict
{'Jason': [(2014, 394, 56), (2013, 350, 23)], 'Stephanie': [(2014, 3, 335), (2013, 21, 217)]}
</code></pre>