Python 生成器行为

Python generator behaviour(Python 生成器行为)
本文介绍了Python 生成器行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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))

这篇关于Python 生成器行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

python arbitrarily incrementing an iterator inside a loop(python在循环内任意递增迭代器)
Joining a set of ordered-integer yielding Python iterators(加入一组产生 Python 迭代器的有序整数)
Iterating over dictionary items(), values(), keys() in Python 3(在 Python 3 中迭代字典 items()、values()、keys())
What is the Perl version of a Python iterator?(Python 迭代器的 Perl 版本是什么?)
How to create a generator/iterator with the Python C API?(如何使用 Python C API 创建生成器/迭代器?)
Splitting a string into 2-letter segments(将字符串拆分为 2 个字母的段)