Think Python Exercise 10.7

编写一个叫做 has_duplicates 的函数,接受一个列表作为参数,如果一个元素在列表中出现了不止一次,则返回 True 。这个函数不能改变原列表。

解答:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def has_duplicates(t):
    p = []
    for i in t:
        if i not in p:
            p.append(i)
        else:
            return True
    return False

t1 = [1,2,3]

has_duplicates(t1)