在Python中递归检查列表唯一性

在Python中递归检查列表唯一性,python,list,recursion,unique,Python,List,Recursion,Unique,我有一个100个数字的列表,我正在尝试编写一个函数来确定所有数字是否都是唯一的(它们应该是唯一的)。我的函数是unique(lst),它接受一个列表作为输入,如果列表中的所有数字都是唯一的,则应该返回True。哦,错了,否则,举个例子: >>> unique([1, 2, 3]) True >>> unique([2, 4, 3, 4]) False 到目前为止,我写了以下内容: def unique(lst): if len(lst) == 1:

我有一个100个数字的列表,我正在尝试编写一个函数来确定所有数字是否都是唯一的(它们应该是唯一的)。我的函数是
unique(lst)
,它接受一个列表作为输入,如果列表中的所有数字都是唯一的,则应该返回True。哦,错了,否则,举个例子:

>>> unique([1, 2, 3])
True

>>> unique([2, 4, 3, 4])
False
到目前为止,我写了以下内容:

def unique(lst):
    if len(lst) == 1:
        return True
    else:
        if lst[0] in lst[1:]:
            return False
        if lst[1] in unique(lst[2:-1]):
            return False
        else:
            return True 
我在递归检查lst[0]之后的数字是否唯一时遇到问题。有人能告诉我如何编辑代码以使其正确检查吗?

这个怎么样

def unique(lst):
    if len(lst) == 1:
        return True
    else:
        currentElement = lst[0]
        remaining = lst[1:]
        elementIsUnique = currentElement not in remaining
        recursive = unique(remaining)
        return elementIsUnique and recursive

>>> unique([1, 2, 3])
True

>>> unique([2, 4, 3, 4])
False
这个怎么样

def unique(lst):
    if len(lst) == 1:
        return True
    else:
        currentElement = lst[0]
        remaining = lst[1:]
        elementIsUnique = currentElement not in remaining
        recursive = unique(remaining)
        return elementIsUnique and recursive

>>> unique([1, 2, 3])
True

>>> unique([2, 4, 3, 4])
False

您只需在列表中不断递归移动,无需返回布尔值:

def unique(lst):
    if len(lst) == 1: # if a single element left, list has unique elements
        return True
    elif lst[0] in lst[1:]: # check if current element is in the remainder of the list
            return False
    else:
        return unique(lst[1:]) # move to next element

您只需在列表中不断递归移动,无需返回布尔值:

def unique(lst):
    if len(lst) == 1: # if a single element left, list has unique elements
        return True
    elif lst[0] in lst[1:]: # check if current element is in the remainder of the list
            return False
    else:
        return unique(lst[1:]) # move to next element

unique()
返回一个布尔值,因此unique(…)中的
lst[1]不起作用。为什么在递归时忽略最后一项呢?如果你想删除你的问题,有更好的方法。但是,一般来说,如果问题是高质量且可回答的,则不应删除这些问题——StackOverflow的目的是建立并维护一个通用的,其他人可以搜索和学习可重用的知识库。还要注意的是,服务条款规定,在StackOverflow上发布内容意味着网站获得该内容的永久许可。有关删除数据指南的讨论,请参阅;一般来说,规则是,只有在“没有任何持久价值”的情况下,才应删除问题。这就是说,这些问题通常都是一开始就不应该被问到的您需要这种递归方法有什么原因吗?更清晰、更高效的方法是只在列表上循环一次,并使用
集合
跟踪已看到的值。
unique()
返回布尔值,因此unique(…)中的
lst[1]将不起作用。为什么在递归时忽略最后一项呢?如果你想删除你的问题,有更好的方法。但是,一般来说,如果问题是高质量且可回答的,则不应删除这些问题——StackOverflow的目的是建立并维护一个通用的,其他人可以搜索和学习可重用的知识库。还要注意的是,服务条款规定,在StackOverflow上发布内容意味着网站获得该内容的永久许可。有关删除数据指南的讨论,请参阅;一般来说,规则是,只有在“没有任何持久价值”的情况下,才应删除问题。这就是说,这些问题通常都是一开始就不应该被问到的您需要这种递归方法有什么原因吗?更清楚、更有效的方法是只在列表上循环一次,并使用
集合
跟踪已看到的值。我不确定我是否理解您的问题,是否希望我将
else
案例分为几行,一步一步地进行?好的,我将其分为几步,希望这更清楚。是的,
elementIsUnique
将是一个
bool
。而
recursive
将不断调用
unique
,直到它最终达到
1
的长度,除非
elementIsUnique
变为False,否则它将提前返回。您不能说
return elemtIsunique和return recursive
,因为在一个函数中只能
return
1次,这样写是无效的语法。我不确定我是否理解您的问题,你想让我把
else
案例一步一步地分成几行吗?好的,我把它分成了更多的步骤,希望这更清楚。是的,
elementIsUnique
将是一个
bool
。而
recursive
将不断调用
unique
,直到它最终达到
1
的长度,除非
elementIsUnique
变为False,否则它将提前返回。不能说
returnelemtisunique和returnrecursive
,因为在一个函数中只能
return
1次,这样写是无效的语法。