2016-12-07 11 views
-1

私は長い間解決策を見つけようとしているPythonの基本的な質問が1つありますが、正しい出力が得られません。Pythonの特殊文字に基づいてサブリストに動的リストを分割

textvalues=[['1 of 2 DOCUMENTS', 'The New York Times', 'March 17, 2016 Thursday\xa0\xa0Late Edition - Final', 'Paid Notice: Deaths THORNTON, ROBERT', 'SECTION: Section A; Column 0; Classified; Pg. 19', 'LENGTH: 176 words', 'LOAD-DATE: March 17, 2016', 'Copyright 2016 The New York Times Company', '', '2 of 2 DOCUMENTS', 'The New York Times', 'March 16, 2016 Wednesday\xa0\xa0Late Edition - Final', 'Paid Notice: Deaths THORNTON, ROBERT', 'SECTION: Section B; Column 0; Classified; Pg. 16', 'LENGTH: 176 words', 'LOAD-DATE: March 16, 2016', 'Copyright 2016 The New York Times Company']] 

ここで私は上記のリストを「特殊文字」に基づくサブリストに分割する必要があります。上記のリストはサンプルリストであり、メインリストは動的であり、リストの長さが異なる場合があります。いずれにしても、リストは ""文字で区切る必要があります。私が試してみました

ソリューション:

MainText = str(textvalues) 
split_index = MainText.index('',) 
l2 = MainText[:split_index] 
print(l2) 

予想される解決策:

[['1 of 2 DOCUMENTS', 'The New York Times', 'March 17, 2016 Thursday\xa0\xa0Late Edition - Final', 'Paid Notice: Deaths THORNTON, ROBERT', 'SECTION: Section A; Column 0; Classified; Pg. 19', 'LENGTH: 176 words', 'LOAD-DATE: March 17, 2016', 'Copyright 2016 The New York Times Company'] ,['2 of 2 DOCUMENTS', 'The New York Times', 'March 16, 2016 Wednesday\xa0\xa0Late Edition - Final', 'Paid Notice: Deaths THORNTON, ROBERT', 'SECTION: Section B; Column 0; Classified; Pg. 16', 'LENGTH: 176 words', 'LOAD-DATE: March 16, 2016', 'Copyright 2016 The New York Times Company']] 

は、問題を解決するために私を助けてください。おかげ

+0

右足の解決策を確認します。これは少し変更して動作します。彼の答えに対するコメントで私のコードを見てください。 – MYGz

+0

私の解決策を確認してください。 – MYGz

答えて

1
import itertools 

textvalues=[['1 of 2 DOCUMENTS', 'The New York Times', 'March 17, 2016 Thursday\xa0\xa0Late Edition - Final', 'Paid Notice: Deaths THORNTON, ROBERT', 'SECTION: Section A; Column 0; Classified; Pg. 19', 'LENGTH: 176 words', 'LOAD-DATE: March 17, 2016', 'Copyright 2016 The New York Times Company', '', '2 of 2 DOCUMENTS', 'The New York Times', 'March 16, 2016 Wednesday\xa0\xa0Late Edition - Final', 'Paid Notice: Deaths THORNTON, ROBERT', 'SECTION: Section B; Column 0; Classified; Pg. 16', 'LENGTH: 176 words', 'LOAD-DATE: March 16, 2016', 'Copyright 2016 The New York Times Company']] 
groups = [] 
for a,b in itertools.groupby(textvalues[0], lambda x: x is not ''): 
    if a: 
     groups.append(list(b)) 
print groups 

は出力:

[['1 of 2 DOCUMENTS', 'The New York Times', 'March 17, 2016 Thursday\xa0\xa0Late Edition - Final', 'Paid Notice: Deaths THORNTON, ROBERT', 'SECTION: Section A; Column 0; Classified; Pg. 19', 'LENGTH: 176 words', 'LOAD-DATE: March 17, 2016', 'Copyright 2016 The New York Times Company'], ['2 of 2 DOCUMENTS', 'The New York Times', 'March 16, 2016 Wednesday\xa0\xa0Late Edition - Final', 'Paid Notice: Deaths THORNTON, ROBERT', 'SECTION: Section B; Column 0; Classified; Pg. 16', 'LENGTH: 176 words', 'LOAD-DATE: March 16, 2016', 'Copyright 2016 The New York Times Company']] 
+0

良い解決策。非常にトリッキーです。それを共有してくれてありがとう。 –

0

基本的に、あなたがコンテンツをループ、バッファ内の部分文字列を格納することができ、かつ''セパレータを渡って来たときにメインリストにバッファをダンプ:

result = list() 
line = list() 
for element in textvalues[0]: 
    if element != '': 
     line.append(element) 
    else: 
     result.append(line) 
     line = list() 
+0

ソリューションに対する修正。これをチェックし、あなたの答えを編集してください。 'text '=' [asd '、' '、' asd d '、' '、' d '、' '、' asd f '、' '、' lskd ']] result = [] line = [] textvaluesの要素のための [0]: 要素の場合= '':! line.append(要素)他 : result.append(ライン) ライン= []他 : result.append(ライン) 印刷結果 ' – MYGz

+0

上記のコードの出力は次のとおりです:' [['asd']、['asd d']、['c as d']、['asd f']、['lskd']] ' – MYGz

+0

他にも複数存在するのでエラーになります。 – Mho

0
textvalues=[['1 of 2 DOCUMENTS', 'The New York Times', 'March 17, 2016 Thursday\xa0\xa0Late Edition - Final', 'Paid Notice: Deaths THORNTON, ROBERT', 'SECTION: Section A; Column 0; Classified; Pg. 19', 'LENGTH: 176 words', 'LOAD-DATE: March 17, 2016', 'Copyright 2016 The New York Times Company', '', '2 of 2 DOCUMENTS', 'The New York Times', 'March 16, 2016 Wednesday\xa0\xa0Late Edition - Final', 'Paid Notice: Deaths THORNTON, ROBERT', 'SECTION: Section B; Column 0; Classified; Pg. 16', 'LENGTH: 176 words', 'LOAD-DATE: March 16, 2016', 'Copyright 2016 The New York Times Company']] 

textvalues2 = [] 

for i in ','.join(i for i in textvalues[0]).split(',,') : 
    textvalues2.append(i.split(',')) 
関連する問題