Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 链表结构中的迭代问题_Python_Python 3.x_Data Structures_Linked List_Iterator - Fatal编程技术网

Python 链表结构中的迭代问题

Python 链表结构中的迭代问题,python,python-3.x,data-structures,linked-list,iterator,Python,Python 3.x,Data Structures,Linked List,Iterator,我的链表结构的\uu iter\uu()实现有问题。我想在不使用yield和iter()的情况下实现它 对于一个循环,它可以正常工作,但当我添加嵌套循环时,外部循环仅迭代一次,并在内部循环完成后退出: class LinkedList: class Node: def __init__(self, data, next=None): self.data, self.next = data, next def __init__(self,

我的链表结构的
\uu iter\uu()
实现有问题。我想在不使用
yield
iter()的情况下实现它

对于一个循环,它可以正常工作,但当我添加嵌套循环时,外部循环仅迭代一次,并在内部循环完成后退出:

class LinkedList:
    class Node:
        def __init__(self, data, next=None):
            self.data, self.next = data, next

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

    def __iter__(self):
        return self

    def __next__(self):
        if not self.current:
            raise StopIteration
        else:
            temp = self.current.data
            self.current = self.current.next
            return iter(self.current)

    def extend(self, seq):
        for val in reversed(seq):
            node = self.Node(val, self.head)
            if self.tail is None:
                self.tail = node
            self.head = node

ll = LinkedList(range(10, 101, 10))
for val in ll:
    for val2 in ll:
        print((val, val2))
这会产生错误的输出:

(10,20)(10,30)(10,40)(10,50)(10,60)(10,70)(10,80)(10,90)(10100)


我还希望得到第一个值为20、30等的元组。

嵌套循环的问题如下:

ll = LinkedList([1,2,3,4])
for val in ll:
    for val2 in ll:
        print((val, val2))
…对于嵌套循环,您将拥有相同的迭代器实例,即链表本身,它只有一个
current
属性。因此,当内部循环完成时,
当前
将是
,外部循环将调用
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。所以它退出了

您需要在
\uu iter\uu
功能中远离
返回self
。相反,您应该在每次调用
\uuuu iter\uuuu
时返回唯一的内容

因此,请专门为此定义一个类

注意:您的
\uuuuuuuuuuuuuuu下一个\uuuuuuu
实现的最后一行也有一个错误:它应该返回
temp

class LinkedList:
    class Iterator:
        def __init__(self, current):
            self.current = current

        def __next__(self):
            if not self.current:
                raise StopIteration
            else:
                temp = self.current.data
                self.current = self.current.next
                return temp  # <-- correction

    class Node:
        def __init__(self, data, next=None):
            self.data, self.next = data, next

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

    def __iter__(self):
        return self.Iterator(self.head)  # <--- new instance!

    def extend(self, seq):
        for val in reversed(seq):
            self.head = self.Node(val, self.head)
            if self.tail is None:
                self.tail = self.head

# now the nested iteration works...
ll = LinkedList([1,2,3,4])
for val in ll:
    for val2 in ll:
        print((val, val2))
类链接列表:
类迭代器:
定义初始化(自身,当前):
自身电流=电流
定义下一个(自我):
如果不是自电流:
提出停止迭代
其他:
温度=自身当前数据
self.current=self.current.next

return temp#链表应该在
\uu iter\uu()
上返回一个单独的迭代器对象(属于它自己的类)。它还必须在每次调用时返回一个新的独立对象。这个对象(它的类)必须实现
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
@PatrickArtner对不起,它的