简体   繁体   English

递归-从列表中删除重复项

[英]Recursion- removes duplicates from the list

The question is to write a recursive function that Removes duplicates from the list. 问题是要编写一个递归函数,以从列表中删除重复项。

Hint: it can be implemented with two auxiliary functions. 提示:它可以通过两个辅助功能实现。

The list contains one and only one of each value formerly present in the list. 该列表仅包含列表中以前存在的每个值中的一个。 The first occurrence of each value is preserved. 保留每个值的第一次出现。

Implement and test a recursive version of this method 实现和测试此方法的递归版本

def clean(self):

    key_node = self._front

    while key_node is not None:
        # Loop through every node - compare each node with the rest
        previous = key_node
        current = key_node._next

        while current is not None:
            # Always search to the end of the list (may have > 1 duplicate)
            if current._value == key_node._value:
                # Remove the current node by connecting the node before it
                # to the node after it.
                previous._next = current._next
                self._count -= 1
            else:
                previous = current
            # Move to the _next node.
            current = current._next
        # Check for duplicates of the _next remaining node in the list
        key_node = key_node._next
    return

And this is what I have so far, I don't understand what is auxiliary functions: 这就是到目前为止,我不了解什么是辅助功能:

def clean(list):
   i = 1
   if len(list) == 0:
     return []
   elif len(list) == 1:
      return list
   elif list[i] == list[i-1]:
       del list[i]
    return clean(list[i:])

Example: for list = [1,2,2,3,3,3,4,5,1,1,1] , the answer is [1,2,3,4,5,1] 示例:对于list = [1,2,2,3,3,3,4,5,1,1,1] ,答案是[1,2,3,4,5,1]

Here is one function that calls a another, recursive function and produces the result you want. 这是一个调用另一个递归函数并产生所需结果的函数。 I suppose you could call clean2 an auxiliary function, although, like you, I don't know exactly what that's supposed to mean. 我想您可以将clean2称为辅助函数,尽管像您一样,我也不知道这到底意味着什么。 It's a rather ridiculous piece of code but it doesn't use any standard library functions and it modifies x in place. 这是一段相当荒谬的代码,但是它不使用任何标准库函数,而是在适当位置修改x。 I would expect it to be horribly inefficient. 我希望它效率极低。 If I wrote this for my day job I'd probably resign. 如果我为我的日常工作写这篇文章,我可能会辞职。

def clean(x):
    clean2(x, 1)

def clean2(x, i):
    if i >= len(x):
        return
    if x[i] == x[i - 1]:
        del x[i]
        clean2(x, i)
    else:
        clean2(x,i+1)


the_list = [1,2,2,3,3,3,4,5,1,1,1]
clean(the_list)
print(the_list)

>>> [1,2,3,4,5,1]

Here's a recursive definition using an auxiliary function go to actually implement the recursion. 这是一个使用辅助函数go的递归定义,用于实际实现递归。 The idea is that the outer clean function can take any iterable object (eg a list) and it always produces a list of values: 这个想法是,外部clean函数可以接受任何可迭代的对象(例如,列表),并且始终生成值列表:

def clean(iterable):
    def go(iterator, result):
        try:
            x = next(iterator)
            if result == [] or result[-1] != x:
                result.append(x)
            return go(iterator, result)
        except StopIteration:
            return result

    return go(iter(iterable), [])

clean just invokes a locally-defined go function, passing an iterator and the eventual result value (initially an empty list). clean只是调用一个本地定义的go函数,传递一个迭代器和最终的结果值(最初是一个空列表)。 go then tries to advance the iterator and compares the referenced value with the end of the result : if it's different (or if the result is empty), the value is appended. go然后尝试推进迭代器,并将引用的值与result的末尾进行比较:如果值不同(或结果为空),则将附加值。 go then recurses. go然后递归。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM