Re: insert unique data in a list

Discussion in 'Python' started by Fire Crow, Dec 13, 2009.

  1. Fire Crow

    Fire Crow Guest

    On Dec 13, 11:37 am, mattia <> wrote:
    > How can I insert non-duplicate data in a list? I mean, is there a
    > particular option in the creation of a list that permit me not to use
    > something like:
    > def append_unique(l, val):
    >     if val not in l:
    >         l.append(val)
    >
    > Thanks,
    > Mattia


    You could also define a custom object that manages a custom ordered
    set

    class unique_set(object):
    def __init__(self,list):
    self.list = list

    def __add___(self,val):
    if val not in self.list
    self.list.append(val)
    return True
    return False

    >>> unique_list = unique_set(['a','b','c'])
    >>> unique_list.list

    ['a', 'b', 'c']
    >>> unique_list + 'd'

    True
    >>> unique_list + 'e'

    True
    >>> unique_list + 'd'

    False
    >>> unique_list.list

    ['a', 'b', 'c', 'd', 'e']
    >>>


    I've used this on a few projects, it makes for wonderfully clean code,
    because you can look at your program as an overview without all the
    arithmetic behind it.

    hope it helps
     
    Fire Crow, Dec 13, 2009
    #1
    1. Advertising

  2. On Sun, 13 Dec 2009 10:48:11 -0800, Fire Crow wrote:

    > You could also define a custom object that manages a custom ordered set

    [...]
    > I've used this on a few projects, it makes for wonderfully clean code,
    > because you can look at your program as an overview without all the
    > arithmetic behind it.


    Which is all very good, but beware that your "ordered set" implementation
    is O(N) for insertions, which may be slow once the set grows large enough.

    Also, I'm not sure I like your abuse of the + operator to modify the
    object in place and return a flag. It is an API not shared by (as far as
    I can see) any other data type in Python.


    --
    Steven
     
    Steven D'Aprano, Dec 13, 2009
    #2
    1. Advertising

  3. On Sun, Dec 13, 2009 at 3:09 PM, Steven D'Aprano
    <> wrote:
    > On Sun, 13 Dec 2009 10:48:11 -0800, Fire Crow wrote:
    >
    >> You could also define a custom object that manages a custom ordered set

    > [...]
    >> I've used this on a few projects, it makes for wonderfully clean code,
    >> because you can look at your program as an overview without all the
    >> arithmetic behind it.

    >
    > Which is all very good, but beware that your "ordered set" implementation
    > is O(N) for insertions, which may be slow once the set grows large enough.
    >
    > Also, I'm not sure I like your abuse of the + operator to modify the
    > object in place and return a flag. It is an API not shared by (as far as
    > I can see) any other data type in Python.


    Could probably just abuse an odict as cleanly. The other option that
    leaps to mind is to use a bloom filter and a list.

    Geremy Condra
     
    geremy condra, Dec 13, 2009
    #3
  4. Fire Crow

    Fire Crow Guest

    > Also, I'm not sure I like your abuse of the + operator to modify the
    > object in place and return a flag. It is an API not shared by (as far as
    > I can see) any other data type in Python.


    I agree it couuld be more consisten with other object apis,

    I also think that if every api has to conform to every other api
    nothing will ever get done.

    Heres a slightly more familiar version, it returns the value added or
    none to conform with other APIs.

    class unique_set(object):
    def __init__(self,list):
    self.list = list

    def __add___(self,val):
    if val not in self.list
    self.list.append(val)
    return val
    return None

    >>> unique_list = unique_set(['a','b','c'])
    >>> unique_list.list

    ['a', 'b', 'c']
    >>> unique_list + 'd'

    'd'

    then a test opperand to verify if a value was inserted could be

    if unique_list + 'd':
    # done some stuff

    but it could also be used in cases like this

    unique_added = unique_list + 'd' or 'not added'
     
    Fire Crow, Dec 14, 2009
    #4
  5. Fire Crow

    Lie Ryan Guest

    On 12/14/2009 11:49 AM, Fire Crow wrote:
    > I also think that if every api has to conform to every other api
    > nothing will ever get done.


    but if every object has its own distinct API, we will never finish
    reading the docs (assuming they do exist for each object).
     
    Lie Ryan, Dec 14, 2009
    #5
    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. ToshiBoy
    Replies:
    6
    Views:
    888
    ToshiBoy
    Aug 12, 2008
  2. knifenomad

    Re: insert unique data in a list

    knifenomad, Dec 14, 2009, in forum: Python
    Replies:
    6
    Views:
    435
    Steven D'Aprano
    Dec 14, 2009
  3. deathweaselx86
    Replies:
    5
    Views:
    1,162
    Raymond Hettinger
    Jun 25, 2011
  4. J. Muenchbourg
    Replies:
    3
    Views:
    259
    Aaron Bertrand - MVP
    Sep 30, 2003
  5. Token Type
    Replies:
    9
    Views:
    393
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page