2016-08-10 11 views
1

各リストエントリを分割して複数のリストを作成しようとしています。形式は次のとおりです。/n Python 3で複数のリストのリストエントリを分割する

リストエントリの形式は

['Root Cause: Hardware Failure\nAction Completed: Power supply/filter/cable swap\nArrival Time: 02/01/2014 15:54:17\nLeaving Time: 02/01/2014 16:27:44\nWas the job successful: Yes\nNotes:replaced dsl cable and filter. Also rebooted all equipment. All working fine now.\nNext Action required:none\nAdded by jakubkwasny at 02/01/2014 21:41:40\nPinging 55:55:55:55 with 32 bytes of data:\nReply from 55:55:55:55: bytes=32 time=67ms TTL=240\nReply from 55:55:55:55: bytes=32 time=92ms TTL=240\nReply from 55:55:55:55: bytes=32 time=76ms TTL=240\nReply from 55:55:55:55: bytes=32 time=82ms TTL=240\nPing statistics for 88.55.55.55:\nPackets: Sent = 4, Received = 4, Lost = 0 (0% loss),\nApproximate round trip times in milli-seconds:\nMinimum = 67ms, Maximum = 92ms, Average = 79ms'] 

は基本的に、私はで新しいリストを開始し、このフォーマットに従うと、私はいくつかのリストに各エントリを分割する必要がある私のリストに多くのエントリを持っています各\ n記号。このような:

リスト1 =根本的な原因:ハードウェア障害 LIST2 =アクション完了:電源/フィルタ/ケーブルスワップ

などと、これはエントリの何千もの私は持っているためにこれを行う必要がありますすべてのルートのリストが完了したすべてのアクションのリストを引き起こし、そうとそう

に私は、新しいリストを開始するが、わからないところがあっ

から行きたい各ポイントで/ n個のシンボルを置くために管理していますどんな助けもありがたいです

+1

'LSTを[0] .splitlinesを()' –

+0

はあなたが私たちに期待を示してくださいすることができます出力? – harshil9968

答えて

3

についての詳細を読むことができますし、str.splitlinesは、リスト内のすべての文字列を分割することを個々の行のサブリストに:

split_lines = [s.splitlines() for s in lst] 

リスト内の個々の文字列の場合:

from itertools import chain 

    split_lines = [[s] for s in chain(*map(str.splitlines, lst))] 
0

これはあなたが見ているものですか?

x = ['Root Cause: Hardware Failure\nAction Completed: Power supply/filter/cable swap\nArrival Time: 02/01/2014 15:54:17\nLeaving Time: 02/01/2014 16:27:44\nWas the job successful: Yes\nNotes:replaced dsl cable and filter. Also rebooted all equipment. All working fine now.\nNext Action required:none\nAdded by jakubkwasny at 02/01/2014 21:41:40\nPinging 55:55:55:55 with 32 bytes of data:\nReply from 55:55:55:55: bytes=32 time=67ms TTL=240\nReply from 55:55:55:55: bytes=32 time=92ms TTL=240\nReply from 55:55:55:55: bytes=32 time=76ms TTL=240\nReply from 55:55:55:55: bytes=32 time=82ms TTL=240\nPing statistics for 88.55.55.55:\nPackets: Sent = 4, Received = 4, Lost = 0 (0% loss),\nApproximate round trip times in milli-seconds:\nMinimum = 67ms, Maximum = 92ms, Average = 79ms'] 


y = x[0].split('\n') 
+0

これはアイテムごとのリストを作成しませんが、python 3 'map'のアイテム –

0

あなたはリストのリストたい場合:

list(map(lambda s: [s], old_list[0].splitlines())) 

リストカンプを使用lambda functionsmap builtin

+0

のリストはリストを作成しません。 'list(map(...)) 'のような' list() '関数に渡す必要があります。 Python 3はOPによってタグ付けされているので、更新してください。あなたの答えをアップアップしています –

1

をあなたがアイテムを作成して、そのようにマスターリストにそれらを配置するsplit()メソッドを使用することができます。

alist = ['Root Cause: Hardware Failure\nAction Completed: Power supply/filter/cable swap\nArrival Time: 02/01/2014 15:54:17\nLeaving Time: 02/01/2014 16:27:44\nWas the job successful: Yes\nNotes:replaced dsl cable and filter. Also rebooted all equipment. All working fine now.\nNext Action required:none\nAdded by jakubkwasny at 02/01/2014 21:41:40\nPinging 55:55:55:55 with 32 bytes of data:\nReply from 55:55:55:55: bytes=32 time=67ms TTL=240\nReply from 55:55:55:55: bytes=32 time=92ms TTL=240\nReply from 55:55:55:55: bytes=32 time=76ms TTL=240\nReply from 55:55:55:55: bytes=32 time=82ms TTL=240\nPing statistics for 88.55.55.55:\nPackets: Sent = 4, Received = 4, Lost = 0 (0% loss),\nApproximate round trip times in milli-seconds:\nMinimum = 67ms, Maximum = 92ms, Average = 79ms'] 

new_lists = [] 
for i in alist: 
    for j in i.split('\n'): 
     new_lists.append([j]) 

print(new_lists) 
# [['Root Cause: Hardware Failure'], ['Action Completed: Power supply/filter/cable swap'], ... 

最初forループはあなたの最初のリストが分割される必要がある複数のエントリーを含んでいる場合です。

0

ここに必要なすべてのリスト

list = ['Root Cause: Hardware Failure\nAction Completed: Power supply/filter/cable swap'] 
temp = L[0].split('\n') 
newList = [] 
for n in temp: 
    newList.append([n]) 

のリストを取得し、あなたが得る:

newList = [['Root Cause: Hardware Failure'], ['Action Completed: Power supply/filter/cable swap']] 
関連する問題