如何在Linked List类中实现生成器?

时间:2018-02-12 14:14:12

标签: python python-3.x python-2.7 generator

我正在尝试在Python中实现一个简单的链接列表,并试图使用我在其中学到的所有Python概念。我被困在为班级实现一个Generator。

代码:

list_gen = iter(list1)
print (next(list_gen))
print (next(list_gen))

用法:

def next(self):
    tempNode = self.head
    while tempNode:
        tempNode2 = tempNode
        tempNode = tempNode.nextNode
        return tempNode2.data
    else:
        raise StopIteration

输出:

  

生成器对象接下来是0x7ff885b63960

     

生成器对象接下来是0x7ff885b63960

既不打印节点的数据值,也不维护方法的当前状态[从返回的地址可以看出]。

我在哪里弄错了?提前谢谢。

修改 我将代码修改为:

<project xmlns="..." xmlns:xsi="..."
xsi:schemaLocation="...">
<modelVersion>4.0.0</modelVersion>
<groupId>...</groupId>
<artifactId>SurefireAuswerterBackend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-http</artifactId>
        <version>2.25.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-servlet</artifactId>
        <version>2.25.1</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.2.20.v20161216</version>
            <dependencies>
                <dependency>
                    <groupId>org.eclipse.jetty</groupId>
                    <artifactId>jetty-io</artifactId>
                    <version>9.2.20.v20161216</version>
                </dependency>
            </dependencies>
            <configuration>
                <scanIntervalSeconds>10</scanIntervalSeconds>
                <war>${project.basedir}/target/surefire-auswerter.war</war>
                <stopKey>alpha</stopKey>
                <stopPort>4445</stopPort>
                <httpConnector>
                    <port>4444</port>
                </httpConnector>
                <webAppSourceDirectory>${project.basedir}/target/surefire-auswerter</webAppSourceDirectory>
                <webAppConfig>
                    <contextPath>/surefire-auswerter</contextPath>
                </webAppConfig>
            </configuration>
        </plugin>
    </plugins>
</build>

现在,打印节点值,但如前所述,状态不会保留,每次都会打印第一个节点的值。

1 个答案:

答案 0 :(得分:0)

您需要一个实例变量来维护调用next()之间的迭代器状态。此外,__next__不是生成器函数,它只返回下一个元素,所以在那里使用yield

def __init__(self, ...):
    # ...
    self.iter_node = None  # maintain state of iterator

def __iter__(self):
    return self

def next(self):
    if self.iter_node is None  # reset iterator to allow repeated iteration
        self.iter_node = self.head or 'XXX'  # dummy for to signal end
    if self.iter_node != 'XXX':
        rval = self.iter_node.data
        self.iter_node = self.iter_node.nextNode or 'XXX'
        return rval
    else:
        self.iter_node = None
        raise StopIteration