我正在尝试定义一个消除列表中非唯一元素的函数;然后检查第二个列表是否相等。
def checkio(data):
data = []
data = set(data)
if len(data) > 0:
return data
if __name__ == "__main__":
assert isinstance(checkio([1]),list) "The result must be a list"
assert checkio([1, 2, 3, 1, 3]) == [1, 3, 1, 3]
assert checkio([1, 2, 3, 4, 5]) == []
assert checkio([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5]
assert checkio([10, 9, 10, 10, 9, 8]) == [10, 9, 10, 10, 9]
答案 0 :(得分:5)
您忘记了def d
:
def checkio(data):
和paren:
assert isinstance(checkio([1]), "The result must be a list") # <-
即使有这些修复,你的实例也是错误的,第二个arg应该是类或类型。
您还可以使用数据作为参数名称,然后设置data = []
,这样您就会有一个空列表。
这似乎是您尝试使用collections.Counter dict查找非唯一值的方法:
from collections import Counter
def checkio(data):
# get count of each element in data
cn = Counter(data)
# keep elements from data that are non unique
# iterating over data to keep the order
return [k for k in data if cn[k] > 1]
if __name__ == "__main__":
# isinstance needs a class or type
assert isinstance(checkio([1]), list)
assert checkio([1, 2, 3, 1, 3]) == [1, 3, 1, 3]
assert checkio([1, 2, 3, 4, 5]) == []
assert checkio([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5]
assert checkio([10, 9, 10, 10, 9, 8]) == [10, 9, 10, 10, 9]
假设你真的希望你的断言通过。