擅长:python、mysql、java
<p>使用<code>itertools.groupby()</code>按键值对词典进行分组,然后从每个组中获取第一个项。</p>
<pre><code>import itertools
data =[ {
"ID" : "0001",
"Organization" : "SolarUSA",
"Matchcode" : "SolarUSA, Something Street, Somewhere State, Whatev Zip",
"Owner" : "Timothy Black",
}, {
"ID" : "0002",
"Organization" : "SolarUSA",
"Matchcode" : "SolarUSA, Something Street, Somewhere State, Whatev Zip",
"Owner" : "Johen Wilheim",
}, {
"ID" : "0003",
"Organization" : "Zapotec",
"Matchcode" : "Zapotec, Something Street, Somewhere State, Whatev Zip",
"Owner" : "Simeon Yurrigan",
} ]
print [g.next() for k,g in itertools.groupby(data, lambda x: x['Matchcode'])]
</code></pre>
<p>给出结果</p>
<pre><code>[{'Owner': 'Timothy Black',
'Organization': 'SolarUSA',
'ID': '0001',
'Matchcode': 'SolarUSA, Something Street, Somewhere State, Whatev Zip'},
{'Owner': 'Simeon Yurrigan',
'Organization': 'Zapotec',
'ID': '0003',
'Matchcode':'Zapotec, Something Street, Somewhere State, Whatev Zip'}]
</code></pre>
<p>我相信这就是你要找的。</p>
<p>编辑:我更喜欢独特的解决方案。它更短更具描述性。</p>