方法1:使用set函数,s=set(list),然后再list(s).
方法2:使用字典函数:
a=[1,2,4,2,4,5,6,5,7,8,9,0]
b={}
b=b.fromkeys(a)
c=list(b.keys())
print('去重后的list为:',c)
方法3:append
def delList(L):
L1 = []
for i in L:
if i not in L1:
L1.append(i)
return L1
print(delList([1,2,2,3,3,4,5]))
print(delList([1,8,8,3,9,3,3,3,3,3,6,3]))
方法4:count,remove
def delList(L):
for i in L:
if L.count(i) != 1:
for x in range((L.count(i)-1)):
L.remove(i)
return L
print(delList([1,2,2,3,3,4,5]))
print(delList([1,8,8,3,9,3,3,3,3,3,6,3]))
方法5:
a=[1,2,4,2,4,5,7,10,5,5,7,8,9,0,3]
a.sort()
last=a[-1]
for i in range(len(a)-2,-1,-1):
if last==a[i]:
del a[i]
else:
last=a[i]
print(a)