删除重复的数字而不设置python 3

时间:2018-04-02 20:53:11

标签: python python-3.x

我想删除没有设置的重复数字。我想使用def函数,还有什么东西吗?

"."

2 个答案:

答案 0 :(得分:0)

x =[1,3,4,5,5,5,5,6,8,9]

def removeDuplicates(array):
    new = []
    for item in array:
        if item not in new:
            new.append(item)
    return new

print(removeDuplicates(x))

答案 1 :(得分:0)

如果订购了清单:

x = [1, 3, 4, 5, 5, 5, 5, 6, 8, 9]
print(x)  # [1, 3, 4, 5, 5, 5, 5, 6, 8, 9]
y = [v for i, v in enumerate(x) if x[i-1] != v or i==0]
print(y)  # [1, 3, 4, 5, 6, 8, 9]