Python,nltk正在向两个不同的列表返回相同的列表值?

2024-06-08 06:41:51 发布

您现在位置: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)

Tags: 数据代码in文档support列表forif
1条回答
网友
1楼 · 发布于 2024-06-08 06:41:51

(.. 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)都被计算为真值


你的意思是将^{}与生成器表达式一起使用吗

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

相关问题 更多 >

    热门问题