2014-07-05 52 views
0

我想將文檔分成兩個列表,但是這兩個列表中的代碼給了我相同的數據。我定義了兩個單獨的字典,這些字典也是我在這裏使用的。Python,nltk是否將相同的列表值返回給兩個不同的列表?

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

你可以把它所有的列表理解'裏面[文件句子的句子,如果任何(句子字一個字在support_ethos)]' –

回答

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)和​​被評估爲真。


您的意思是使用any來生成表達式嗎?

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

謝謝你,我會嘗試。並且你對我在哪裏可以研究更多關於編碼情感分析 – user3753567

+0

@ user3753567有什麼建議,我不瞭解情緒分析。 – falsetru

相關問題