仅使用Lambda函数删除重复项 [英] Removing duplicates using only lambda functions

查看:348
本文介绍了仅使用Lambda函数删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题 Python-仅通过使用filter和lambda 可以删除列表中的重复项,OP会询问如何仅使用filterlambda函数从Python列表中删除重复项.

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.

这让我感到奇怪,从理论的角度来看,是否可以仅使用lambda函数从Python列表中删除重复项?

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?

在这种情况下,删除重复项"的意思是在原始列表中每个元素仅保留一次出现",因此[1,2,1,3,1,4]应该变成[1,2,3,4].

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].

此外,目标是仅编写一个lambda,因此代码将是单行代码,例如:

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.

此外,对于上述问题,不允许花哨",尤其是set函数以及reducemap ...

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.

推荐答案

从理论上讲,如果计算问题需要输入和输出而没有副作用,则lambda演算可能可以解决它(更一般而言,lambda演算已完成Turing,请参见维基百科).

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).

现在要执行,下面的lambda函数接受一个list参数,并返回一个列表,其中所有重复项都已被删除:

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)

这是未包装的版本:

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
    )

该函数包含以下递归函数的lambda版本:

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:]))

要模拟递归调用,等效的lambda会接受一个附加函数作为参数,该函数本身就是

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:]))
               )

然后,另一个lambda调用此先前的函数,将自身作为参数传递(除列表外):

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

lambda u, a: u(u, a)

最后,外部lambda将所有内容包装起来,只需要一个列表作为参数.

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

这篇关于仅使用Lambda函数删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆