简体   繁体   English

如何将列表按逗号划分为令牌

[英]How can I divide a List into tokens by comma

Hi i am really new to python. 嗨,我真的是python新手。 I would like to ask whether based on my code below, how can I actually divide the list, inst, into tokens by comma 我想问一下是否基于下面的代码,如何才能将列表inst按逗号划分为令牌

print("ID - actual - predicted")
for index, inst in enumerate(test):
    print "\n", inst
    pred = cls.classify_instance(inst)
    dist = cls.distribution_for_instance(inst)
    print(
        "%d - %s - %s" %
        (index+1, inst.get_string_value(inst.class_index)
         ,inst.class_attribute.value(int(pred))))
split_inst = inst.split(",")

So what I try to do is splitting it by comma. 所以我想做的是用逗号分割它。 But I keep on getting error like 'Instance' object has no attribute 'split' like this: 但是我一直在遇到类似“实例”对象没有属性“ split”的错误,如下所示:

22,in,in,1,PP,21,prep,IN,pr,null,null,null,null,null,null,the,DT,det,def,null,null,sg,null,null,system,NN,null,null,null,in,IN,recorded,VBN,NN,?
95 - ? - nonerror

Traceback (most recent call last):
 File "C:\Python FYP\Tkinter\prediction.py", line 44, in <module>
  main(sys.argv)
 File "C:\Python FYP\Tkinter\prediction.py", line 38, in main
 split_inst = inst.split(',')
AttributeError: 'Instance' object has no attribute 'split'

How can I solve this problem? 我怎么解决这个问题?

By looking at your question, you are using object for split function. 通过查看您的问题,您正在将对象用于拆分功能。 so copy the object string into different variable and then try to split it. 因此,将对象字符串复制到其他变量中,然后尝试将其拆分。

Because objects can not splitted, it has to be string 由于对象无法拆分,因此必须为字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM