且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除列表中的重复项,同时保持其顺序(Python)

更新时间:2023-11-01 20:55:04

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

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