我有一个列表,其中包含以不同单词结尾和开头的句子。你知道吗
我想实现以下目标:
<p>
开头和结尾,则追加到新列表<p>
开始,但不以<p>
结束,则附加到临时字符串并检查下一行。如果下一行没有以<p>
结尾,请将其附加到临时字符串,直到到达以<p>
结尾的行['<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>']
我知道这是琐碎的,似乎很容易,但我的时间很短,我需要一个快速修复。谢谢
从一个列表开始,根据条件追加或合并。不需要临时字符串:
当代码运行时会得到以下结果:
这也可以通过^{} 实现:
相关问题 更多 >
编程相关推荐