擅长:python、mysql、java
<p>我制作了一个函数来获取dict列表的所有唯一值</p>
<pre><code>def unique_values_from_list(dict_list):
all_values = set()
for dictionary in dict_list:
all_values.update(dictionary.values())
return all_values
</code></pre>
<p>使用此代码,我们可以获得两组唯一值,并找到这两组值的交点:</p>
<pre><code>list_1 = [{'a':'cat', 'b':'dog', 'c':'bird'},{'a':'mouse', 'b':'lizard', 'c':'fish'},{'a':'hen', 'b':'pony', 'c':'frog'}]
list_2 = [{'x':'goat', 'y':'mouse', 'z':'horse'},{'x':'horse', 'y':'hen', 'z':'tiger'},{'x':'bee', 'y':'fly', 'z':'toad'}]
unique1 = unique_values_from_list(list_1)
unique2 = unique_values_from_list(list_2)
print(unique1)
print(unique2)
intersection = unique1.intersection(unique2)
print(intersection)
</code></pre>
<p>我的结果是:</p>
<pre><code>unique1: {'fish', 'cat', 'frog', 'dog', 'lizard', 'hen', 'pony', 'bird', 'mouse'}
unqiue2: {'goat', 'fly', 'horse', 'hen', 'toad', 'mouse', 'tiger', 'bee'}
intersection: {'hen', 'mouse'}
</code></pre>