Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
540 views
in Technique[技术] by (71.8m points)

python - Removing duplicates using only lambda functions

I came across the question Python - Removing duplicates in list only by using filter and lambda, where the OP asks how to remove duplicate elements from a Python list using exclusively filter and lambda functions.

This made me wonder, is it possible, from a theoretical point of view, to remove the duplicates from a Python list using only lambda functions?

If so, how can we do that?

In this case, "removinge the duplicates" means "keeping exactly one occurrence of each element present in the original list", so [1,2,1,3,1,4] should become [1,2,3,4].

In addition, the goal is to write only one lambda, so the code would be a one-liner like:

lambda l: """do something that returns l without duplicates"""

No external variable must be used.

Besides, as for the above question, nothing "fancy" is allowed, especially the set function, as well as reduce, map...

Basically, no other function, even the built-in, should be called.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From a theoritecal point of view, if a computational problem requires an input and an output without side-effect, lambda calculus can probably solve it (more generally, lambda calculus is Turing complete, cf wikipedia).

Now for the implementation, the following lambda function takes a list argument, and returns a list where all the duplicates have been removed:

lambda l: (lambda u, a: u(u, a)) ((lambda f, x: x if len(x) <= 0 else (f(f, x[1:]) if x[0] in x[1:] else ([x[0]] + f(f, x[1:])))), l)

Here is an unwrapped version:

lambda l:
    (lambda u, a: u(u, a))
    (
        (lambda f, x: x if len(x) <= 0
                        else
                        (
                            f(f, x[1:]) if x[0] in x[1:]
                                        else ([x[0]] + f(f, x[1:]))
                        )
         ),
         l
    )

The function consists in a lambda version of the following recursive function:

def f(l):
    if len(l) <= 0:
        return l
    elif l[0] in l[1:]:
        return f(l[1:])
    else:
        return ([l[0]] + f(l[1:]))

To emulate a recursive call, the equivalent lambda takes an additional function as argument, that will be itself:

lambda f, x: x if len(x) <= 0
               else
               (
                   f(f, x[1:]) if x[0] in x[1:]
                               else ([x[0]] + f(f, x[1:]))
               )

Then, another lambda calls this previous function, passing itself as argument (besides the list):

lambda u, a: u(u, a)

Finally, an outer lambda wraps everything up, that takes only a list as argument.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...