方法1(先排序再遍历)
def removeSame(L,x=0):
L.sort() #把列表按从小到大的顺序排列
num = len(L)-1
for x in range(num):
if L[x] == L[x+1]:
L.remove(L[x+1])
return removeSame(L,x)
print(L)
L = [1,2,2,8,4,6,7,2,3,3,4,]
removeSame(L)
方法2(set方法)
def removeSame2(L):
print(list(set(L)))
L = [1,2,2,8,4,6,7,2,3,3,4,]
removeSame2(L)
方法3(字典键方法)
def removeSame3(L):
print(list(dict.fromkeys(L)))
L = [1,2,2,8,4,6,7,2,3,3,4,]
removeSame3(L)
方法4(遍历)
def removeSame4(L):
l = []
for x in L:
if x not in l:
l.append(x)
print(l)
L = [1,2,2,8,4,6,7,2,3,3,4,]
removeSame4(L)