Python 3.x Python用户列表模块

Python 3.x Python用户列表模块,python-3.x,Python 3.x,我是Python新手。我正在考虑使用一个名为Ulist的类来重写add、append和extend方法,这样重复的值就不会被这些操作添加到列表中。下面是我的代码。但问题是,当我试图打印我的列表时,结果是一个空列表。你能帮我知道我的代码哪部分是错的吗?谢谢 class Ulist(UserList): def __init__(self, data): UserList.__init__(self) self.list = data def __a

我是Python新手。我正在考虑使用一个名为Ulist的类来重写add、append和extend方法,这样重复的值就不会被这些操作添加到列表中。下面是我的代码。但问题是,当我试图打印我的列表时,结果是一个空列表。你能帮我知道我的代码哪部分是错的吗?谢谢

class Ulist(UserList):
    def __init__(self, data):
        UserList.__init__(self)
        self.list = data

    def __add__(self, newdata):
        for i in newdata:
            if i in self.list:
                print (i, "not be added to the list")
            else:
                self.list += [i]
        return self.list

    def append(self,newdata):
        for i in newdata:
            if i in self.list:
                 print (i, "not be added to the list")
            else:
                self.list.append(i)
        return self.list

    def extend(self,newdata):
         for i in newdata:
            if i in self.list:
                  print (i, "not be added to the list")
            else:
                self.list.extend(i)
         return self.list

mylist = Ulist([1,2,3])

mylist.__add__([1,2])
print (mylist)

重构你的self.list->self.data

class Ulist(UserList):
    def __init__(self, data):
        UserList.__init__(self)
        self.data = data

    def __add__(self, newdata):
        for i in newdata:
            if i in self.data:
                print (i, "not be added to the list")
            else:
                self.data += [i]
        return self.data

    def append(self, newdata):
        for i in newdata:
            if i in self.data:
                 print (i, "not be added to the list")
            else:
                self.data.append(i)
        return self.data

    def extend(self, newdata):
         for i in newdata:
            if i in self.data:
                  print (i, "not be added to the list")
            else:
                self.data.extend(i)
         return self.data

mylist = Ulist([1,2,3])

mylist.__add__([1,2])
print (mylist)

UserList
不将其数据存储为
self.list
它将自身存储为
self.data

from collections import UserList
class Ulist(UserList):
    def __init__(self, data):
        UserList.__init__(self)
        self.data = data

    def __add__(self, newdata):
        for i in newdata:
            if i in self.data:
                print(i, "not be added to the list")
            else:
                self.data += [i]
        return self.data

    def append(self, newdata):
        for i in newdata:
            if i in self.data:
                 print(i, "not be added to the list")
            else:
                self.data.append(i)
        return self.data

    def extend(self, newdata):
         for i in newdata:
            if i in self.data:
                  print(i, "not be added to the list")
            else:
                self.data.extend(i)
         return self.data


mylist = Ulist([1, 2, 3])

mylist.__add__([1, 2])
print(mylist)
输出:

1 not be added to the list
2 not be added to the list
[1, 2, 3]

非常感谢你!