面接問題---Pythonコードを書いてリストの重複要素を削除してください.

1221 ワード

1: set ,s=set(list), list(s). 2: : 
   a=[1,2,4,2,4,5,6,5,7,8,9,0]
     b={}
     b=b.fromkeys(a)
     c=list(b.keys())
     print('    list :',c)

3:append
def delList(L):
    L1 = []
    for i in L:
        if i not in L1:
            L1.append(i)

    return L1    

print(delList([1,2,2,3,3,4,5]))
print(delList([1,8,8,3,9,3,3,3,3,3,6,3]))

メソッド4:count,remove
def delList(L):
    for i in L:
        if L.count(i) != 1:
            for x in range((L.count(i)-1)):
                L.remove(i) 

    return L

print(delList([1,2,2,3,3,4,5]))
print(delList([1,8,8,3,9,3,3,3,3,3,6,3]))

5:
a=[1,2,4,2,4,5,7,10,5,5,7,8,9,0,3]
a.sort()
last=a[-1]
for i in range(len(a)-2,-1,-1):
    if last==a[i]:
        del a[i]
    else:
        last=a[i]

print(a)