Python 如何创建元组列表?

Python 如何创建元组列表?,python,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 n

您好,我正在尝试创建以下格式的元组列表:

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)
简单地做:

nbTrain = []

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


nbTrain.append(word)

只需使用列表:

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

您可以将其缩短一点:[输入文本,如果为正输入文本,则为“pos”,对于数据源中的输入文本,则为“neg”]您不需要引用输入文本两次。非常正确!我更喜欢你的,真不敢相信这么简单。你是个救生员!非常感谢。