标签:for 函数 dex index highlight code ems port imp
list去重
方法1:利用循环去重
s= [1,1,2,2,2,3,4,4] res= [] for i in s: if i not in res: res.append(i) print(res) s1= list(set(s)) print(s1)
方法2:利用字典去重
res_d ={} for i in s: if i in res_d: res_d[i] += 1 else: res_d[i] =1 print(res_d) for k,v in res_d.items(): print(k)
方法3:set去重,再转化成list
print(list(set(s)))
方法4:通过删除index
s = [1,1,1,2,2,3,4,4] t= s[1:] for i in s: while t.count(i) > 1: del t[t.index(i)] print(t)
方法5:利用reduce、lambda函数去重
from functools import reduce l1 = [1,1,1,2,2,3,4,4,4] func = lambda x,y:x if y in x else x+[y] print(reduce(func,[[],]+l1))
标签:for 函数 dex index highlight code ems port imp
原文地址:https://www.cnblogs.com/ff-gaofeng/p/11124796.html