去除列表中不重复的元素 分类: python 小练习 2013-06-25 14:59 245人阅读 评论(0) 收藏
'''
去除列表中不重复的元素,且保证重复元素的次序与原列表一致
'''
def checkio(data):
l =[]
for i in data:
if data.count(i)>1:
l.append(i)
return l
print checkio([1, 2, 3, 1, 3]) == [1, 3, 1, 3], "1st example"
print checkio([1, 2, 3, 4, 5]) == [], "2nd example"
print checkio([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5], "3rd example"
print checkio([10, 9, 10, 10, 9, 8]) == [10, 9, 10, 10, 9], "4th example"
去除列表中不重复的元素,且保证重复元素的次序与原列表一致
'''
def checkio(data):
l =[]
for i in data:
if data.count(i)>1:
l.append(i)
return l
#These "asserts" using only for self-checking and not necessary for auto-testing
print checkio([1, 2, 3, 1, 3]) == [1, 3, 1, 3], "1st example"
print checkio([1, 2, 3, 4, 5]) == [], "2nd example"
print checkio([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5], "3rd example"
print checkio([10, 9, 10, 10, 9, 8]) == [10, 9, 10, 10, 9], "4th example"
版权声明:本文为博主原创文章,未经博主允许不得转载。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步