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 生成器行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)