2017-06-18 52 views
0

我想用.join()方法将字符串列表加入到一个字符串中。然而,看起来结果仍然是输出仍然产生一个字母表的列表,而不是一个字。下面是培训相关代码:我怎样才能将一个字符串列表加入到Python中的一个字符串中

import sys 
import traceback 
import weka.core.jvm as jvm 
#import wekaexamples.helper as helper 
from weka.core.converters import Loader 
from weka.classifiers import Classifier 


def main(args): 
# load a dataset 
loader = Loader(classname="weka.core.converters.ArffLoader") 
train = loader.load_file("C:/Users/Syahirah/Desktop/Train_FYP.arff") 
train.class_index = train.num_attributes - 1 
test = loader.load_file("C:/Users/Syahirah/Desktop/Test_FYP_prediction.arff") 
test.class_is_last() 

# classifier 
cls = Classifier(classname="weka.classifiers.bayes.NaiveBayes") 
cls.build_classifier(train) 

# output predictions 
#print("ID - actual - predicted ") 
for index, inst in enumerate(test): 
    #print "\n", inst 
    varinst = str(inst) 
    #print type(varinst),varinst 
    split_inst = varinst.split(',') 
    kata = split_inst[1] 
    semua = ' '.join(kata) 
    print semua 

这里的输出:

T h e 
c u s t o m e r s 
a r e 
a l l o w e d 
t o 
r e s e r v e 
r o o m 
o n l i n e 
' 
b y 
t h e 
w a y 
t h e y 
a b l e 
t o 
c h a n g e 
o r 
c a n c e l 
t h e i r 
r e s e r v a t i o n 

所需的输出应该是这样的:

The customers are allowed to reserve room online, by the way they able to 
change or cancel their reservation 
+0

你的'test'变量包含什么?这并没有太多的继续。 –

+0

@速度实例变量 – Lily

+0

'test'变量包含具有变量的对象列表。在这段代码中,我提取出第二个变量“word”。从单词列表中,我想用完整的句子来表达它。我对解释感到非常抱歉。这是不好解释,因为我真的是新的python。 – Lily

回答

2

在你的.py文件的第一行添加此行:

from __future__ import print_function 

接下来,将这些行更改S:

semua = ' '.join(kata) 
print semua 

要:

semua = ''.join(kata) 
print(semua, end='') 

你要确保你不添加空格和地方不需要换行。

+0

哇!谢谢 – Lily

+0

@Lily不用担心。提问时所需要的只是一点点清晰。 ;) –

相关问题