-
巧妙使用reduce
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]
-
如果不保持顺序,只需要转为set即可
ids = list(set(ids))
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]
ids = list(set(ids))
本文标题:【转】python list 保持顺序去重复
本文链接:https://www.haomeiwen.com/subject/lpclcttx.html
网友评论