如果条目不是以数字开头,我想将一个条目与前一个条目连接起来
例如:
l = ["1. first paragraph", "2. second paragraph", "end of second paragraph", "3. third paragraph"]
result = []
curr_str = ""
for item in l:
curr_str += item
if not item[0].isdigit():
result.append(curr_str)
curr_str = ""
What I want
result = ["1. first paragraph", "2. second paragraphend of second paragraph", "3. third paragraph"]
What I have
result=["1. first paragraph2. second paragraphend of second paragraph"]
你可以使用负索引来获取你想要的
l = ["1. first paragraph", "2. second paragraph", "end of second paragraph", "3. third paragraph"]
res = []
for i in l:
if i[0].isdigit():
res.append(i)
else:
res[-1] = res[-1] + i
print(res)
输出['1. first paragraph', '2. second paragraphend of second paragraph', '3. third paragraph']
注意:
如果你的第一个元素不是以数字开头,这将不起作用。
如果列表为空或第一个字符为数字,则需要更改if条件,如if not res or i[0].isdigit():
。
一种方法可能是将列表作为单个字符串连接在一起,然后用空格分隔,后跟一个数字段落头:
import re
l = ["1. first paragraph", "2. second paragraph", "end of second paragraph", "3. third paragraph"]
inp = ' '.join(l)
paragraphs = re.split(r's+(?=d+.)', inp)
print(paragraphs)
这个打印:
['1. first paragraph',
'2. second paragraph end of second paragraph',
'3. third paragraph']
在将当前项连接到curr_str
之前,需要检查当前项是否以数字开头。
在循环结束时,您需要检查curr_str
是否包含任何内容,因此您可以将最后的项目附加到列表中。
l = ["1. first paragraph", "2. second paragraph", "end of second paragraph", "3. third paragraph"]
result = []
curr_str = ""
for item in l:
if item[0].isdigit():
if curr_str:
result.append(curr_str)
curr_str = ""
curr_str += item
if curr_str:
result.append(curr_str)
print(result)
CODE
连接列表中的所有元素,您必须使用"".join(list)
,但在您的情况下,它必须更具选择性,因此我们可以使用列表推导:
l = ["1. first paragraph", "2. second paragraph", "end of second paragraph", "3. third paragraph"]
import re
result = [elem for elem in l if re.match('^d.*$', elem)]
result = "n".join(result)
print(result)
输出:
1. first paragraph
2. second paragraph
3. third paragraph
解释
第一步:
['1. first paragraph2. second paragraph3. third paragraph']
使用列表推导式和正则表达式过滤输入列表中的值,并得到一个仅包含以数字开头的字符串的列表
输出:
['1. first paragraph', '2. second paragraph', '3. third paragraph']
第二步:
result = ["n".join(result)]
使用join
内置字符串方法连接列表项
好了
l = ["1. first paragraph", "2. second paragraph", "end of second paragraph", "3. third paragraph"]
result = []
curr_str = l[0]
for item in l[1:]:
if item[0].isdigit():
result.append(curr_str)
curr_str = item
else:
curr_str+=item
result.append(curr_str)
这将工作。我真的不能评论你的解决方案,因为我不理解它背后的思考过程,重新开始。如果您有任何问题,请随时提问