[Tutor] removing duplicates

Christopher Spears cspears2002 at yahoo.com
Fri Mar 5 17:47:59 EST 2004


I have made more progress on my probelm.  Here is the
code so far:

import UserList

class UList(UserList.UserList):
    def __init__(self, initlist=None):
        UserList.UserList.__init__(self, initlist)

    def __add__(self, other):
        for s in range(len(self)):
            if self[s] not in other:
                if isinstance(other, UList):
                    return self.__class__(self.data +
other.data)
                elif isinstance(other,
type(self.data)):
                    return self.__class__(self.data +
other)
                else:
                    return self.__class__(self.data +
list(other))
            else:
                print "Found duplicate %s" %self[s]
                del self[s]

When I first tested the script out, everything seemed
to work fine:
>>> import UList
>>> list= [1,2,3]
>>> a = UList.UList(list)
>>> a
[1, 2, 3]
>>> list1 = [1,4,5]
>>> b = UList.UList(list1)
>>> b
[1, 4, 5]
>>> a + b
Found duplicate 1
[2, 3, 1, 4, 5]

However, I have been discovering situations where the
script breaks.  For example, in cases where there are
more than one duplicate:
>>> list2 = [1,2,4]
>>> c = UList.UList(list2)
>>> c
[1, 2, 4]
>>> b
[1, 4, 5]
>>> c + b
Found duplicate 1
Found duplicate 4
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in -toplevel-
    c + b
  File "C:\Documents and Settings\Christstopher
Spears\My Documents\python\UList.py", line 9, in
__add__
    if self[s] not in other:
  File "C:\Python23\lib\UserList.py", line 28, in
__getitem__
    def __getitem__(self, i): return self.data[i]
IndexError: list index out of range


>>> d
[8, 9]
>>> f = [8,8,10]
>>> d + f
Found duplicate 8

Traceback (most recent call last):
  File "<pyshell#23>", line 1, in -toplevel-
    d + f
  File "C:\Documents and Settings\Christstopher
Spears\My Documents\python\UList.py", line 9, in
__add__
    if self[s] not in other:
  File "C:\Python23\lib\UserList.py", line 28, in
__getitem__
    def __getitem__(self, i): return self.data[i]
IndexError: list index out of range

I am feeling a little silly because one would think
that an IndexError would be easy to solve.  However, I
have no idea what is going on!  Any hints or
suggestions?!

-Chris



More information about the Tutor mailing list