python实现七种列表去重方法

#encoding=utf-8
import time
time_start=time.time()
print u"列表去重的七种方法"
print u"第一种测试方法"
repeat_list=[1,2,4,1,5,1,2,5]
result=[]
for i in repeat_list:
    if i not in result:
        result.append(i)
print u"第一种去重结果: ",result
print u"第二种测试方法"
repeat_list=[1,2,4,1,5,1,2,5]
result={}
print u"第二种去重结果: ",list(result.fromkeys(repeat_list))
print u"第三种测试方法"
repeat_list=[1,2,4,1,5,1,2,5]
print u"第三种去重结果: ",list(set(repeat_list))
print u"第四种测试方法"
repeat_list=[1,2,4,1,5,1,2,5]
import itertools
def test_groupby(x):
    if x==1:
        print "lower"
    elif x>1 and x<4:
        print "middle"
    elif x>=4:
        print "higher"
repeat_list=sorted(repeat_list)
data=itertools.groupby(repeat_list,key=test_groupby)
for i,j in data:
    print list(j)
data=itertools.groupby(repeat_list)
result=[]
for i,j in data:
    result.append(i)
print u"第四种去重结果: ",result
print u"第五种测试方法"
repeat_list=[1,2,4,1,5,1,2,5]
for i in [j for j in repeat_list if repeat_list.count(i)>1]:
    for x in range(repeat_list.count(i)-1):
     repeat_list.remove(i)
print u"第五种去重结果: ",repeat_list
print u"第六种测试方法"
repeat_list=[1,2,4,1,5,1,2,5]
i=0
while i<=len(repeat_list)-1:
    if repeat_list.count(repeat_list[i])>1:
        repeat_list.pop(i)
    else:
        i+=1
print u"第六种去重结果: ",repeat_list
print u"第七种测试方法"
repeat_list=[1,2,4,1,5,1,2,5]
func=lambda x,y:x if y in x else x + [y]
print u"第七种去重结果: ",reduce(func,[[],]+repeat_list)
print "_"*20
print u"去重程序耗时%f" % (time.time()-time_start)
print "_"*20
time.sleep(3)
posted @ 2019-07-14 21:41  张特别  阅读(3614)  评论(0编辑  收藏  举报