RE: Question on lists

Discussion in 'Python' started by Dan, Jul 28, 2004.

  1. Dan

    Dan Guest

    I've got a SortedList class I wrote with a unique() member function that
    does just this. I'm going to post it to the Python cookbook soon. If
    you're interested I can send along a copy.

    -Dan

    -----Original Message-----
    From: python-list-bounces+dan=
    [mailto:python-list-bounces+dan=] On Behalf Of
    Kristofer Pettijohn
    Sent: Tuesday, July 27, 2004 8:27 PM
    To:
    Subject: Question on lists

    My question is about lists:

    Is there a way to remove duplicate items from a list that are
    next to each other?

    Example...

    Performing the operation on ['a', 'b', 'c', 'c', 'd', 'd', 'd', 'e']
    will return ['a', 'b', 'c', 'd', 'e']

    Performing the operation on ['a', 'b', 'c', 'c', 'd', 'c', 'd', 'e']
    will return ['a', 'b', 'c', 'd', 'c', 'd', 'e']

    I'm guessing there is probably nothing internal that will do it, so
    I may have to write something on my own - just thought I'd check
    first ;)

    Thanks!

    --
    Kristofer Pettijohn

    --
    http://mail.python.org/mailman/listinfo/python-list
     
    Dan, Jul 28, 2004
    #1
    1. Advertising

  2. Dan

    Tony Clarke Guest

    A simple list comprehension approach:

    def condense(m):
    print [m[k] for k in range(len(m)) if m[k]!=m[k-1]]

    l=[1,2,2,4,3,5,5,6]
    condense(l)
    gives:
    >>>

    [1, 2, 4, 3, 5, 6]
    >>>


    Cheers
    Tony Clarke

    "Dan" <> wrote in message news:<>...
    > I've got a SortedList class I wrote with a unique() member function that
    > does just this. I'm going to post it to the Python cookbook soon. If
    > you're interested I can send along a copy.
    >
    > -Dan
    >
    > -----Original Message-----
    > From: python-list-bounces+dan=
    > [mailto:python-list-bounces+dan=] On Behalf Of
    > Kristofer Pettijohn
    > Sent: Tuesday, July 27, 2004 8:27 PM
    > To:
    > Subject: Question on lists
    >
    > My question is about lists:
    >
    > Is there a way to remove duplicate items from a list that are
    > next to each other?
    >
    > Example...
    >
    > Performing the operation on ['a', 'b', 'c', 'c', 'd', 'd', 'd', 'e']
    > will return ['a', 'b', 'c', 'd', 'e']
    >
    > Performing the operation on ['a', 'b', 'c', 'c', 'd', 'c', 'd', 'e']
    > will return ['a', 'b', 'c', 'd', 'c', 'd', 'e']
    >
    > I'm guessing there is probably nothing internal that will do it, so
    > I may have to write something on my own - just thought I'd check
    > first ;)
    >
    > Thanks!
    >
    > --
    > Kristofer Pettijohn
    >
     
    Tony Clarke, Jul 31, 2004
    #2
    1. Advertising

  3. Dan

    Peter Otten Guest

    Tony Clarke wrote:

    > A simple list comprehension approach:
    >
    > def condense(m):
    > print [m[k] for k in range(len(m)) if m[k]!=m[k-1]]
    >
    > l=[1,2,2,4,3,5,5,6]
    > condense(l)
    > gives:
    > >>>

    > [1, 2, 4, 3, 5, 6]
    >>>>


    That does not work for len(m) == 1 and for m[0] == m[-1]:

    >>> def condense(m):

    .... print [m[k] for k in range(len(m)) if m[k]!=m[k-1]]
    ....
    >>> condense([1,2,1]) # expected: [1,2,1]

    [2, 1]
    >>> condense([42]) # expected: [42]

    []

    Peter
     
    Peter Otten, Jul 31, 2004
    #3
  4. Dan

    Tony Clarke Guest

    Peter Otten <> wrote in message news:<cefd6i$q21$00$-online.com>...
    > Tony Clarke wrote:
    >
    > > A simple list comprehension approach:
    > >
    > > def condense(m):
    > > print [m[k] for k in range(len(m)) if m[k]!=m[k-1]]
    > >
    > >Snip

    >
    > That does not work for len(m) == 1 and for m[0] == m[-1]:
    >
    > >>> def condense(m):

    > ... print [m[k] for k in range(len(m)) if m[k]!=m[k-1]]
    > ...
    > >>> condense([1,2,1]) # expected: [1,2,1]

    > [2, 1]
    > >>> condense([42]) # expected: [42]

    > []
    >
    > Peter


    Oops! more testing was indicated!
    How's this:

    def condense(m):
    print m, 'condensed is',
    print [m[0]]+[m[k] for k in range(1,len(m)) if m[k]!=m[k-1]]

    l=[1,2,2,4,3,5,5,6]
    condense(l)
    n=[1,2,1]
    m=[42]
    o=[0,0,1,2,3,3,0,0]
    condense (n)
    condense (m)
    condense(o)

    >>>

    [1, 2, 2, 4, 3, 5, 5, 6] condensed is [1, 2, 4, 3, 5, 6]
    [1, 2, 1] condensed is [1, 2, 1]
    [42] condensed is [42]
    [0, 0, 1, 2, 3, 3, 0, 0] condensed is [0, 1, 2, 3, 0]
    >>>

    Cheers
    Tony Clarke
     
    Tony Clarke, Jul 31, 2004
    #4
    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. JustSomeGuy

    Sorting lists of lists...

    JustSomeGuy, Jun 17, 2004, in forum: C++
    Replies:
    0
    Views:
    342
    JustSomeGuy
    Jun 17, 2004
  2. Jon Slaughter

    lists of lists

    Jon Slaughter, Dec 13, 2004, in forum: C++
    Replies:
    4
    Views:
    453
    Buster
    Dec 13, 2004
  3. Charlotte Henkle

    Counter for items in lists in lists?

    Charlotte Henkle, Sep 25, 2004, in forum: Python
    Replies:
    8
    Views:
    417
    Charlotte Henkle
    Sep 26, 2004
  4. robin
    Replies:
    10
    Views:
    564
    Dave Hansen
    Apr 12, 2006
  5. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    442
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
Loading...

Share This Page