Python 如何解决没有循环或递归,令人费解?

Python 如何解决没有循环或递归,令人费解?,python,list,Python,List,我想做一个函数,当有两个相同的元素时,将列表作为输入并返回值。列表是排序的。我不能使用任何循环或递归。有没有可能的方法 def continuity(lst): if len(lst)==1: return 'no' elif lst[0]==lst[1]: return 'yes' else: return continuity(lst[1:]) 这就是我所做的,但它使用递归。为相同的对象创建一个集合,然后比较长度: d

我想做一个函数,当有两个相同的元素时,将列表作为输入并返回值。列表是排序的。我不能使用任何循环或递归。有没有可能的方法

def continuity(lst):
    if len(lst)==1:
        return 'no'
    elif lst[0]==lst[1]:
        return 'yes'
    else:
        return continuity(lst[1:])

这就是我所做的,但它使用递归。

为相同的对象创建一个集合,然后比较长度:

def continuity(lst):
    lst_set = set(lst)
    return 'yes' if len(lst_set) == len(lst) and len(lst) != 1 else 'no'
使用
计数器

def continuity(lst):
    from collections import Counter
    occurrences = list(Counter(x).values())
    sum = sum(occurrences)
    return 'yes' if sum == len(lst_set) and len(lst) != 1 else 'no'

价值观是什么?数字还是字符串?值是数字使用一个集合只包含唯一值的事实。为什么命名函数
continuity
?如果有,我想你误解了这个问题。如果lst的len为1,有没有其他方法会失败
def continuity(lst):
    if len(lst) > 1
        if len(lst) == len(set(lst)):
            return 'yes'
        else:
            return 'no'
    else:
        return 'no'