2016-02-16 21 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

非常真实!我更喜欢你的。 –