删除列表中的重复项,同时保持其顺序(Python) [英] Remove duplicates in a list while keeping its order (Python)

查看:67
本文介绍了删除列表中的重复项,同时保持其顺序(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这实际上是此问题的扩展.删除重复项后,该问题的答案未保留列表的顺序". 如何从列表中删除这些重复项(python)

This is actually an extension of this question. The answers of that question did not keep the "order" of the list after removing duplicates. How to remove these duplicates in a list (python)

biglist = 

[ 

    {'title':'U2 Band','link':'u2.com'}, 
    {'title':'Live Concert by U2','link':'u2.com'},
    {'title':'ABC Station','link':'abc.com'}

]

在这种情况下,应该删除第二个元素,因为先前的"u2.com"元素已经存在.但是,应该保留顺序.

In this case, the 2nd element should be removed because a previous "u2.com" element already exists. However, the order should be kept.

推荐答案

我对另一个问题的答案(您完全忽略了它!)表明您声称

My answer to your other question, which you completely ignored!, shows you're wrong in claiming that

该问题的答案没有 保持订单"

The answers of that question did not keep the "order"

  • 我的回答没有保持秩序,显然是的.再次强调,看您是否可以一直忽略它...:
    • my answer did keep order, and it clearly said it did. Here it is again, with added emphasis to see if you can just keep ignoring it...:
    • 对于一个非常大的列表,可能是最快的方法,如果要保留剩余项目的确切顺序,请执行以下操作:

      Probably the fastest approach, for a really big list, if you want to preserve the exact order of the items that remain, is the following...:

      biglist = [ 
          {'title':'U2 Band','link':'u2.com'}, 
          {'title':'ABC Station','link':'abc.com'}, 
          {'title':'Live Concert by U2','link':'u2.com'} 
      ]
      
      known_links = set()
      newlist = []
      
      for d in biglist:
        link = d['link']
        if link in known_links: continue
        newlist.append(d)
        known_links.add(link)
      
      biglist[:] = newlist
      

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

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