如何在列表中删除这些重复项(Python)
biglist =
[
{'title':'U2 Band','link':'u2.com'},
{'title':'ABC Station','link':'abc.com'},
{'title':'Live Concert by U2','link':'u2.com'}
]
我想删除列表中的THIRD元素...因为它具有重复的"u2.com".我不想重复的链接"元素.这样做的最有效的代码是什么?
I would like to remove the THIRD element inside the list...because it has "u2.com" as a duplicate. I don't want duplicate "link" element. What is the most efficient code to do this so that it results in this:
biglist =
[
{'title':'U2','link':'u2.com'},
{'title':'ABC','link':'abc.com'}
]
我尝试了许多方法,包括使用许多嵌套的"for ... in ....",但这效率很低而且太长了.
I have tried many ways, including using many nested "for ...in ...." but this is very inefficient and too long.
您可以使用每个字典的link
字段作为排序键对列表进行排序,然后对列表进行一次遍历并删除重复项(或者,创建一个删除了重复项的新列表,Python习惯用法就是这样,
You can sort the list, using the link
field of each dictionary as the sort key, then iterate through the list once and remove duplicates (or rather, create a new list with duplicates removed, as is the Python idiom), like so:
# sort the list using the 'link' item as the sort key
biglist.sort(key=lambda elt: elt['link'])
newbiglist = []
for item in biglist:
if newbiglist == [] or item['link'] != newbiglist[-1]['link']:
newbiglist.append(item)
此代码将为您提供任何重复项"组的第一个元素(原始biglist
中的相对顺序).之所以如此,是因为Python使用的.sort()
算法保证是稳定的排序-它不会更改确定为彼此相等的元素的顺序(在这种情况下,元素具有相同的link
).
This code will give you the first element (relative ordering in the original biglist
) for any group of "duplicates". This is true because the .sort()
algorithm used by Python is guaranteed to be a stable sort -- it does not change the order of elements determined to be equal to one another (in this case, elements with the same link
).