在Python中如何将一个字符串列表连接成一个字符串

2024-05-15 23:09:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用.join()方法将字符串列表连接到一个字符串中。但是,结果似乎仍然是一样的,输出仍然生成一个字母表列表,而不是一个单词。以下是相关代码:

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

Tags: 字符串coretestimport列表loadtrainloader
1条回答
网友
1楼 · 发布于 2024-05-15 23:09:38

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

from __future__ import print_function

接下来,更改这些行:

semua = ' '.join(kata)
print semua

收件人:

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

您要确保没有在不需要的地方添加空格和换行符。你知道吗

相关问题 更多 >