Python 我怎样才能使我的列表更容易阅读?

Python 我怎样才能使我的列表更容易阅读?,python,list,class,functional-programming,nodes,Python,List,Class,Functional Programming,Nodes,我想创建一个行为类似于列表的类。挑战在于不使用列表或字典。到目前为止,我创建了一个节点类,如下所示: class Node: def __init__(self, value=None): self.next = None self.last = None self.value = valor def __repr__(self): return self.value class MyList: de

我想创建一个行为类似于列表的类。挑战在于不使用列表或字典。到目前为止,我创建了一个节点类,如下所示:

class Node:

    def __init__(self, value=None):
        self.next = None
        self.last = None
        self.value = valor

    def __repr__(self):
        return self.value
class MyList:

    def __init__(self):
        self.head = None
        self.tail = None

    def __iter__(self):
        return self

    def __next__(self):
        if self.head:
            if self.head.next:
                self.head = self.head.next
                return self.head.last
            aux = self.head
            self.head = None
            return aux
        raise StopIteration
class MyList:

    def __init__(self):
        self.head = None
        self.tail = None
        self.current = self.head

    def __iter__(self):
        return self

    def __next__(self):
        if self.current is not None:
            it = self.current
            self.current = self.current.next
            return it
        raise StopIteration
MyList
类,它基本上是一个节点链,有一个头部节点和一个尾部节点。问题是,我想让它变得更适合我,这样我就可以用它跑一圈了。我搜索了iter和next的工作原理,得出如下结论:

class Node:

    def __init__(self, value=None):
        self.next = None
        self.last = None
        self.value = valor

    def __repr__(self):
        return self.value
class MyList:

    def __init__(self):
        self.head = None
        self.tail = None

    def __iter__(self):
        return self

    def __next__(self):
        if self.head:
            if self.head.next:
                self.head = self.head.next
                return self.head.last
            aux = self.head
            self.head = None
            return aux
        raise StopIteration
class MyList:

    def __init__(self):
        self.head = None
        self.tail = None
        self.current = self.head

    def __iter__(self):
        return self

    def __next__(self):
        if self.current is not None:
            it = self.current
            self.current = self.current.next
            return it
        raise StopIteration
它可以工作,但它显然删除了
MyList
中的数据,因此我不能再次使用它。关于如何在不弄乱对象内部信息的情况下获得相同的结果,有什么建议吗?

您需要在类中添加一个“当前”标记,以指示迭代当前指向的节点。大概是这样的:

class Node:

    def __init__(self, value=None):
        self.next = None
        self.last = None
        self.value = valor

    def __repr__(self):
        return self.value
class MyList:

    def __init__(self):
        self.head = None
        self.tail = None

    def __iter__(self):
        return self

    def __next__(self):
        if self.head:
            if self.head.next:
                self.head = self.head.next
                return self.head.last
            aux = self.head
            self.head = None
            return aux
        raise StopIteration
class MyList:

    def __init__(self):
        self.head = None
        self.tail = None
        self.current = self.head

    def __iter__(self):
        return self

    def __next__(self):
        if self.current is not None:
            it = self.current
            self.current = self.current.next
            return it
        raise StopIteration
现在,您的列表没有区分其头部和当前迭代位置,但这是两个完全不同的概念

当然,如果这样做,同一MyList上的所有迭代都将被“链接”,因此如果您执行以下操作:

x = MyList(1, 2, 3, 4)
for item in x:
    print(x)
    if item == 2:
        break

for item in x:
    print(x)

然后,第二次迭代将在第一次停止的地方进行。如果不希望出现这种行为,则必须创建一个单独的迭代器类,并让
MyList.\uu iter\uuu
返回该迭代器的实例,而不是
self
。如果从
返回
self
,则对象不能有多个独立的迭代,因为迭代状态存储在对象中,作为正在迭代的数据。

请注意,迭代器协议只要求返回迭代器;您还可以将
\uuuu iter\uuu
实现为生成器,而不是返回实例本身:

def __iter__(self):
    node = self.head
    while node is not None:
        yield node
        node = node.next

你不会产生任何结果吗?@RasmusDamgaardNielsen:
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。我真的不明白发电机是怎么工作的。我已经试过几次了,最后以一个循环结束。@dm295@dm295在python3中语法改为NEXT最后有人使用了
\uuuuuu iter\uuuuu
作为一个生成器函数,定义一个新类真是太过分了。非常感谢@jornsharpe!其他答案也非常有用,但这是最简单的一个:)