请写出一段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]))

 





 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM