回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有一个按特定键排序的词典列表。每个字典包含32个元素,列表中有4000多个字典。我需要代码来处理这个列表,并返回一个新的列表,删除所有重复项。</p>
<p>这些链接中的方法:</p>
<ul>
<li><a href="https://stackoverflow.com/questions/7961363/python-removing-duplicates-in-lists">Removing duplicates in lists</a></li>
<li><a href="https://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-python-whilst-preserving-order">How do you remove duplicates from a list in whilst preserving order?</a></li>
</ul>
<p>别帮我,因为字典不好用。</p>
<p>有什么想法吗?如果你需要更多的信息,评论和我会添加的信息。</p>
<p><strong>编辑</strong>:</p>
<p>重复字典是任何两个具有相同值的字典。</p>
<hr/>
<p>好的,这里有一个详细的解释给那些需要它的人。</p>
<p>我有一个字典列表,比如:</p>
<pre><code>[ {
"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",
} ]
</code></pre>
<p>在这个列表中,第一个和第二个词典是重复的,因为它们的<code>Matchcodes</code>是相同的。</p>
<p>现在,此列表按以下代码排序:</p>
<pre><code># sort_by is "Matchcode"
def sort( list_to_be_sorted, sort_by ):
return sorted(list_to_be_sorted, key=lambda k: k[sort_by])
</code></pre>
<p>所以我有一个按<code>Matchcode</code>排序的词典的整洁列表。现在我只需要遍历列表,访问<code>list[dictionary][key]</code>,并在两个键值匹配时删除重复项。</p>