gpt4 book ai didi

python - 根据开始和结束字符python在列表中附加行

转载 作者:行者123 更新时间:2023-11-28 22:19:05 24 4
gpt4 key购买 nike

我有一个列表,其中包含以不同单词结尾和开头的句子。

我想实现以下目标:

  1. 如果一行以 <p> 开始和结束, 附加到新列表
  2. 如果行以 <p> 开头但不以 <p> 结尾, 附加到临时字符串并检查下一行。如果下一行不以 <p> 结尾,将其附加到临时字符串,直到到达以 <p> 结尾的行
  3. 刷新临时字符串并重复步骤 1 和 2。

工作 list :

['<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>']

我知道这很简单而且看起来很简单,但我时间不够,我需要快速修复。谢谢

最佳答案

从列表开始,然后根据条件追加或连接。不需要临时字符串:

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>

关于python - 根据开始和结束字符python在列表中附加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50033353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com