removing common elemets in a list

Discussion in 'Python' started by saif.shakeel@gmail.com, May 16, 2007.

  1. Guest

    Hi,
    Suppose i have a list v which collects some numbers,how do i
    remove the common elements from it ,without using the set() opeartor.
    Thanks
    , May 16, 2007
    #1
    1. Advertising

  2. Tim Golden Guest

    wrote:
    > Hi,
    > Suppose i have a list v which collects some numbers,how do i
    > remove the common elements from it ,without using the set() opeartor.


    Is this a test? Why don't you want to use the set operator?
    Anyway, you can just move things from one list into another
    excluding those which are already moved:

    <code>
    numbers = [1, 2, 3, 3, 4, 4, 5]
    unique_numbers = []
    for n in numbers:
    if n not in unique_numbers:
    unique_numbers.append (n)

    print unique_numbers
    </code>

    It won't be the fastest thing you could do, but it
    does work. Using a dictionary would speed things up,
    but then you're basically implementing a set using
    a dictionary.

    TJG
    Tim Golden, May 16, 2007
    #2
    1. Advertising

  3. Gary Herron Guest

    wrote:
    > Hi,
    > Suppose i have a list v which collects some numbers,how do i
    > remove the common elements from it ,without using the set() opeartor.
    > Thanks
    >
    >

    Several ways, but probably not as efficient as using a set. (And why
    don't you want to use a set, one wonders???)



    >>> l = [1,2,3,1,2,1]




    Using a set:

    >>> set(l)

    set([1, 2, 3])



    Building the list element by element:

    >>> for e in l:

    .... if e not in r:
    .... r.append(e)
    ....
    >>> print r

    [1, 2, 3]



    Using a dictionary:

    >>> d = dict(zip(l,l))
    >>> d

    {1: 1, 2: 2, 3: 3}
    >>> d.keys()

    [1, 2, 3]
    >>>
    Gary Herron, May 16, 2007
    #3
  4. On Tue, 2007-05-15 at 23:17 -0700, wrote:
    > Hi,
    > Suppose i have a list v which collects some numbers,how do i
    > remove the common elements from it ,without using the set() opeartor.
    > Thanks


    If the list is sorted, you can weed out the duplicates with
    itertools.groupby:

    >>> import itertools
    >>> L = [1,2,3,3,4,4,5]
    >>> [k for (k,_) in itertools.groupby(L, lambda x:x)]

    [1, 2, 3, 4, 5]

    HTH,

    --
    Carsten Haese
    http://informixdb.sourceforge.net
    Carsten Haese, May 16, 2007
    #4
  5. John Zenger Guest

    On May 16, 2:17 am, wrote:
    > Hi,
    > Suppose i have a list v which collects some numbers,how do i
    > remove the common elements from it ,without using the set() opeartor.
    > Thanks


    Submit this as your homework answer -- it will blow your TA's mind!

    import base64
    def uniq(v):
    return
    eval(base64.b64decode('c29ydGVkKGxpc3Qoc2V0KHYpKSxrZXk9bGFtYmRhIHg6di5pbmRleCh4KSk='),locals())
    John Zenger, May 16, 2007
    #5
  6. On May 16, 2007, at 10:36 AM, John Zenger wrote:

    > On May 16, 2:17 am, wrote:
    >> Hi,
    >> Suppose i have a list v which collects some numbers,how do i
    >> remove the common elements from it ,without using the set() opeartor.
    >> Thanks

    >
    > Submit this as your homework answer -- it will blow your TA's mind!
    >
    > import base64
    > def uniq(v):
    > return
    > eval(base64.b64decode
    > ('c29ydGVkKGxpc3Qoc2V0KHYpKSxrZXk9bGFtYmRhIHg6di5pbmRleCh4KSk='),local
    > s())


    Nice! But I think he said he couldn't use set() ;-)
    Michael Bentley, May 16, 2007
    #6
  7. janislaw Guest

    On May 16, 8:17 am, wrote:
    > Hi,
    > Suppose i have a list v which collects some numbers,how do i
    > remove the common elements from it ,without using the set() opeartor.
    > Thanks


    There was a similar thread on polish python newsletter. The following
    post displays 4 different approaches and explores timings:

    http://groups.google.com/group/pl.comp.lang.python/msg/dc3618b18e63f3c9

    Fortunately, python code is universal.
    janislaw, May 17, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris  Chiasson
    Replies:
    6
    Views:
    612
    Richard Tobin
    Nov 14, 2006
  2. Replies:
    3
    Views:
    395
    Joseph Kesselman
    Oct 17, 2007
  3. Denny
    Replies:
    1
    Views:
    774
  4. John Reye
    Replies:
    28
    Views:
    1,363
    Tim Rentsch
    May 8, 2012
  5. Jason Mellone
    Replies:
    3
    Views:
    90
    Jurko Gospodnetić
    May 7, 2014
Loading...

Share This Page