Python 生成器行为

2022-01-10 00:00:00 python generator iterator

问题描述

import itertools
ws=[]
subs=[]
set_subs=[]
for i in xrange(int(raw_input())):
    S=raw_input()
    l=len(S)
    subs.append(S[i:j+1] for i in xrange(l) for j in xrange(i,l))

输入:

2
aab
aac

现在 subs[0]subs[1] 都给了我相同的结果.

now both subs[0] and subs[1] give me same result.

print list(subs[0])
>>>['a','aa','aac','a','ac','c']
print list(subs[1])
>>>['a','aa','aac','a','ac','c']

list(subs[0]) 应该是 ['a','aa','aab','a','ab','b']

我隐约明白为什么会这样.我该怎么做才能使 subs[0]subs[1] 真正不同.

I vaguely understand why this is happening. What do I do to make subs[0] and subs[1] actually different.

注意:换行

subs.append(S[i:j+1] for i in xrange(l) for j in xrange(i,l))

subs.append([S[i:j+1] for i in xrange(l) for j in xrange(i,l)])

不是一种选择


解决方案

http://docs.python.org/reference/expressions.html#generator-expressions

生成器表达式中使用的变量在以下情况下被延迟计算为生成器对象调用 __next__() 方法(在同一像普通发电机一样时尚).但是,最左边的 for 子句是立即评估,以便可以看到它产生的错误在处理生成器的代码中出现任何其他可能的错误之前表达.无法立即评估后续 for 子句因为它们可能依赖于前面的 for 循环.

Variables used in the generator expression are evaluated lazily when the __next__() method is called for generator object (in the same fashion as normal generators). However, the leftmost for clause is immediately evaluated, so that an error produced by it can be seen before any other possible error in the code that handles the generator expression. Subsequent for clauses cannot be evaluated immediately since they may depend on the previous for loop.

S[i:j+1] 在您执行生成器时进行评估,此时 S 具有最新值.

S[i:j+1] is evaluated when you execute the generator, and at that point S has the latest value.

您可以改用普通的生成器.现在 sssubgen 来说是本地的:

You can use a normal generator instead. Now ss is local to subgen:

import itertools

def subgen(ss):
    l=len(ss)
    for i in xrange(l):
        for j in xrange(i,l):
            yield ss[i:j+1]

subs=[]
for i in xrange(int(raw_input())):
    S=raw_input()
    subs.append(subgen(S))

相关文章