doublons.py

Created by ph-moutou

Created on April 21, 2018

618 Bytes

Élimination des doublons d’une liste, selon 4 méthodes Python différentes. NB: les 2 dernières ne marchent pas à cause de fonctionnalités Python non [encore] implémentées.


def elimine1(L):
    P=[]
    for e in L:
        if e not in P:P.append(e)
    return P

def elimine2(L):
    for e in L:
        if L.count(e)>1:
            for i in range(L.count(e)-1):
               L.remove(e) 
    return L

def elimine3(L):#ne marche pas du fait de enumerate
    P=[e for r,e in enumerate(L) if L.index(e)==r] 
    return P

def elimine4(L):#ne marche pas du fait de set
    P=set()
    for e in L: P.add(e)
    return list(P)

L1=[1,2,5,8,9,5,2,1,6,4,8,2,4,2,5,4,6,25,15,1,25,3,6,7,8,49,1,2,6]
L2=elimine1(L1)
print("la liste ne contient que",len(L2),"valeurs differentes",L2)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.