请写出一段Python代码实现删除一个list里面的重复元素?

方法1:使用set函数 

s=set(list),然后再list(s)

方法2:append   

1 def delList(L):
2     L1 = []
3     for i in L:
4         if i not in L1:
5             L1.append(i)
6     return L1    
7  
8 print(delList([1,2,2,3,3,4,5]))
9 print(delList([1,8,8,3,9,3,3,3,3,3,6,3]))

方法3:count,remove   

1 def delList(L):
2     for i in L:
3         if L.count(i) != 1:
4             for x in range((L.count(i)-1)):
5                 L.remove(i) 
6     return L
7 
8 print(delList([1,2,2,3,3,4,5]))
9 print(delList([1,8,8,3,9,3,3,3,3,3,6,3]))





猜你喜欢

转载自www.cnblogs.com/lmh001/p/9790359.html