我想将文档分成两个列表,但这段代码在两个列表中都给出了相同的数据。我在这里定义了两个单独的词典。
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)
答案 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):
...