Добавление строк в список на основе начальных и конечных символов python - PullRequest
0 голосов
/ 26 апреля 2018

У меня есть список, содержащий предложения, которые заканчиваются и начинаются с разных слов.

Я хочу добиться следующего:

  1. Если строка начинается и заканчивается на <p>, добавить в новый список
  2. Если строка начинается с <p>, но не заканчивается <p>, добавьте временную строку и проверьте следующую строку.Если следующая строка не заканчивается на <p>, добавляйте ее во временную строку, пока не доберетесь до строки, заканчивающейся <p>
  3. Обновите временную строку и повторите шаги 1 и 2.

Рабочий список:

['<p>University Press, Inc.',
'The Game of Hearts: Harriette Wilson &amp; 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 &amp; 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>']

Я знаю, что это тривиально и кажется простым, ноУ меня мало времени, и мне нужно быстро исправить.Спасибо

Ответы [ 2 ]

0 голосов
/ 26 апреля 2018

Это также можно сделать с помощью itertools.groupby:

from itertools import groupby

output = []

for test, lines in groupby(gtext, lambda x: x.startswith('<p>') and x.endswith('<p>')):
    if not test:
        output.append(' '.join(list(lines)))
    else:
        output.extend(list(lines))

for line in output:
    print line
# <p>University Press, Inc. The Game of Hearts: Harriette Wilson &amp; 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>
0 голосов
/ 26 апреля 2018

Начните со списка и добавляйте или объединяйте в зависимости от условия.Временная строка не нужна:

workingList = ... #assume its a list of strings. If its not just split it by newlines.
result = []
for i in workingList:
    if '<p>' == i[:3]: result.append(i) #start new if <p> found as start
    else: result[-1] += ' ' + i #add it to the end of the last one

for i in result:
    print(i)

И вы получите эти результаты при запуске кода:

<p>University Press, Inc.The Game of Hearts: Harriette Wilson &amp; 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 tothis book: the research, the many discussions, her invaluable help with thetext itself, and, last but not least, her knowledge of the art of seduction, ofwhich 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 steadfastlythroughout 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>
...