2012-02-05 2 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だろうtring methods](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)。 –

答えて

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

+1

マークなし: '[a.split(" | ")[0] for a answers]' – WolframH

+0

ちょうど["lab abcコンピュータアプリケーションのヒューマンマシンインターフェース"] – ChamingaD

関連する問題