2012-02-05 66 views
-1

我有一系列的答案,包括标记。如何拆分文本数组?

语法 - 。?[“ANSWER1 | MARK1,ANSWER2 | MARK2 ...]

answers = ["Human machine interface for lab abc computer applications|3", 
      "A survey of user opinion of computer system response time|4", 
      "The EPS user interface management system|2", 
      "System and human system engineering testing of EPS|1"] 

我需要为每个回答这些拆分,并提取标志我怎么办呢

+0

以下是一些具体的文档你对字符串进行操作,所以第一站将是[S特林方法](http://docs.python.org/library/stdtypes.html#string-methods),特别是'split'方法。您还在使用一个列表,所以请参阅['for'语句](http://docs.python.org/tutorial/controlflow.html#for-statements)和/或[list comprehensions](http:/ /docs.python.org/tutorial/datastructures.html#list-comprehensions)。 – 2012-02-05 13:44:26

回答

5

例如:

>>> [a.split("|") for a in answers] 
[['Human machine interface for lab abc computer applications', '3'], 
['A survey of user opinion of computer system response time', '4'], 
['The EPS user interface management system', '2'], 
['System and human system engineering testing of EPS', '1']] 
+0

谢谢。另外如何获得没有标记的数组。 – ChamingaD 2012-02-05 12:58:04

+1

没有标记:'[a.split(“|”)[0] for a answers]' – WolframH 2012-02-05 12:59:29

+0

只是[“实验室abc计算机应用程序的人机界面”,...] – ChamingaD 2012-02-05 12:59:56