У меня есть список, содержащий предложения, которые заканчиваются и начинаются с разных слов.
Я хочу добиться следующего:
- Если строка начинается и заканчивается на
<p>
, добавить в новый список - Если строка начинается с
<p>
, но не заканчивается <p>
, добавьте временную строку и проверьте следующую строку.Если следующая строка не заканчивается на <p>
, добавляйте ее во временную строку, пока не доберетесь до строки, заканчивающейся <p>
- Обновите временную строку и повторите шаги 1 и 2.
Рабочий список:
['<p>University Press, Inc.',
'The Game of Hearts: Harriette Wilson & Her Memoirs edited by Lesley Blanch. Copyright © 1955 by<p>',
'<p>7<p>',
'<p>Acknowledgments<p>',
'<p>First, I would like to thank Anna Biller for her countless contributions to',
'this book: the research, the many discussions, her invaluable help with the',
'text itself, and, last but not least, her knowledge of the art of seduction, of',
'which I have been the happy victim on numerous occasions.<p>',
'<p>To the memory of my father<p>',
'<p>8<p>',
'<p>I must thank my mother, Laurette, for supporting me so steadfastly',
'throughout this project and for being my most devoted fan.`<p>`',
'<p>I would like to thank Catherine Léouzon, who some years ago intro-',
'duced me to Les Liaisons Dangereuses and the world of Valmont.<p>']
Рабочий код:
itext = []
tempS = ''
for i in range(len(gtext)):
if gtext[i][:3] == '<p>' and gtext[i][-3:] == '<p>':
itext.append(gtext[i])
elif gtext[i][:3] == '<p>' and gtext[i][-3:] != '<p>':
tempS += gtext[i]
if gtext[i+1][-3:] != '<p>':
tempS += ' ' + gtext[i+1]
if gtext[i+1][-3:] == '<p>':
tempS += ' ' + gtext[i+1]
itext.append(tempS)
tempS = ''
Ожидаемый результат:
['<p>University Press, Inc. The Game of Hearts: Harriette Wilson & Her Memoirs edited by Lesley Blanch. Copyright © 1955 by<p>',
'<p>7<p>',
'<p>Acknowledgments<p>',
'<p>First, I would like to thank Anna Biller for her countless contributions to this book: the research, the many discussions, her invaluable help with the text itself, and, last but not least, her knowledge of the art of seduction, of which I have been the happy victim on numerous occasions.<p>',
'<p>To the memory of my father<p>',
'<p>8<p>',
'<p>I must thank my mother, Laurette, for supporting me so steadfastly throughout this project and for being my most devoted fan.`<p>`',
'<p>I would like to thank Catherine Léouzon, who some years ago intro-duced me to Les Liaisons Dangereuses and the world of Valmont.<p>']
Я знаю, что это тривиально и кажется простым, ноУ меня мало времени, и мне нужно быстро исправить.Спасибо