如何在列表中删除这些重复项(Python) [英] How to remove these duplicates in a list (python)

查看:62
本文介绍了如何在列表中删除这些重复项(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).

这篇关于如何在列表中删除这些重复项(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆