2016-02-16 63 views
-1

你好,我想創建的元組在此格式的信息列表:如何創建元組列表Python?

train = [ 
    ('I love this sandwich.', 'pos'), 
    ('This is an amazing place!', 'pos'), 
    ('I feel very good about these beers.', 'pos'), 
    ('This is my best work.', 'pos'), 
    ("What an awesome view", 'pos'), 
    ('I do not like this restaurant', 'neg'), 
    ('I am tired of this stuff.', 'neg'), 
    ("I can't deal with this", 'neg'), 
    ('He is my sworn enemy!', 'neg'), 
    ('My boss is horrible.', 'neg') 
] 

所以基本上我有一個for循環並返回一個字符串,我想一個「POS」或「NEG」添加到該字符串並創建這些元組的列表。

我嘗試了不同的組合,但仍然不是我想要的結果。任何暗示將非常感激

這是我的代碼:

if classifier.positiv > classifier.negativ: 
    word = (input_text , 'pos') 
else: 
    word = (input_text , 'neg') 


nbTrain.extend(word) 
nbTrain = tuple(nbTrain) 

回答

2

簡單地做:

nbTrain = [] 

if classifier.positiv > classifier.negativ: 
    word = (input_text , 'pos') 
else: 
    word = (input_text , 'neg') 


nbTrain.append(word) 
+0

我簡直不敢相信那很簡單。你是一個拯救生命的人!謝謝。 – Pca

0

只需用一個列表理解:

train = [(input_text, 'pos') if is_positive(input_text) else (input_text, 'neg') for input_text in datasource] 
+1

可以縮短一點:'[input_text,'pos'if is_positive(input_text)else'neg')for input_text in datasource]''''''''''''''''''''''''''''''''不需要兩次引用'input_text'。 – zondo

+0

非常真實!我更喜歡你的。 –