Python,nltk将相同的列表值返回到两个不同的列表?

时间:2014-07-05 14:38:02

标签: python

我想将文档分成两个列表,但这段代码在两个列表中都给出了相同的数据。我在这里定义了两个单独的词典。

def feature_extractor(document):
support_features = []
attack_features = []
for sentence in document:
    if (word in sentence for word in supporting_ethos):
        support_features.append(sentence)
    if (word in sentence for word in attacking_ethos):
        attack_features.append(sentence)
return(attack_features , support_features)

1 个答案:

答案 0 :(得分:2)

(.. for .. in ...)generator expression

>>> (x for x in [1,2])
<generator object <genexpr> at 0x0000000002A1B828>

当它被用作谓词时,它总是被视为真理。

>>> bool(_)
True

所以表达式(word in sentence for word in supporting_ethos)(word in sentence for word in attacking_ethos)都被评估为真实。


您的意思是将any用于生成器表达式吗?

if any(word in sentence for word in supporting_ethos):
    ...