好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python对list列表结构中的值进行去重

今天遇到一个问题,在同事随意的提示下,用了 itertools.groupby 这个函数。不过这个东西最终还是没用上。

ids = [1,2,3,3,4,2,3,4,5,6,1]
news_ids = []
for id in ids:
  if id not in news_ids:
    news_ids.append(id)

print news_ids 
ids = [1,4,3,3,4,2,3,4,5,6,1]
ids = list(set(ids)) 
ids = [1,4,3,3,4,2,3,4,5,6,1]
news_ids = list(set(ids))
news_ids.sort(ids.index) 
ids = [1,4,3,3,4,2,3,4,5,6,1]
ids.sort()
it = itertools.groupby(ids)

for k, g in it:
  print k 
In [5]: ids = [1,4,3,3,4,2,3,4,5,6,1]

In [6]: func = lambda x,y:x if y in x else x + [y]

In [7]: reduce(func, [[], ] + ids)
Out[7]: [1, 4, 3, 2, 5, 6] 

更多Python对list列表结构中的值进行去重相关文章请关注PHP中文网!

查看更多关于Python对list列表结构中的值进行去重的详细内容...

  阅读:59次