以功能方式使Python列表唯一(map/reduce/filter)

Sib*_*ibi 4 python functional-programming map filter

在Python中有没有办法通过功能范例使List独一无二?

输入: [1,2,2,3,3,3,4]

输出:( [1,2,3,4]以保存方式)

我知道还有其他方法,但没有一种是功能性的.

kel*_*nfc 8

如果您只需要删除相邻的事件,请尝试以下方法:

reduce(lambda x,y: x+[y] if x==[] or x[-1] != y else x, your_list,[])
Run Code Online (Sandbox Code Playgroud)

如果您需要删除除一次以外的所有事件,请尝试以下操作:

reduce(lambda x,y: x+[y] if not y in x else x, your_list,[])
Run Code Online (Sandbox Code Playgroud)