我想在字符串“ Title”之后将一长串列表分成几部分。给出的是具有以下结构的列表:
l = ["Title abc", "text1", "text", "Title def", "text4", "text5", "Title ghi", "text4", "text5" ...]
,输出应为:
new = [["Title abc", "text1", "text"], ["Title def", "text4", "text5"], ["Title ghi", "text4", "text5"]]
开始始终是"Title"
,它也是下一场比赛的分隔符。我尝试过
re.findall ('Title.*Title).dx', l, re.DOTALL)
,但这将在"Title ghi"
处停止。你会怎么做?
答案 0 :(得分:4)
类似于以上答案,但可扩展为任意迭代-您可以构建唯一值的生成器,每次满足条件时其值就会递增,然后将其用作itertools.groupby
的一部分来构建子列表,例如:
from itertools import accumulate, groupby
groups = accumulate(el.startswith('Title') for el in data)
out = [[el[1] for el in g] for _, g in groupby(zip(groups, data), lambda L: L[0])]
将给予data
的
['Title abc',
'text1',
'text',
'more text',
'Title def',
'text4',
'Title x',
'Title ghi',
'text4',
'text5']
给您out
:
[['Title abc', 'text1', 'text', 'more text'],
['Title def', 'text4'],
['Title x'],
['Title ghi', 'text4', 'text5']]
答案 1 :(得分:3)
如果列表的格式与您的示例一致,则可以对列表进行切片:
response =
答案 2 :(得分:3)
这也是一个版本:
l = ["Title abc", "text1", "text", "Title def", "text4", "text5", "Title ghi",
"text4", "text5"]
res = []
tmp = []
for item in l:
if item.startswith("Title"):
if tmp:
res.append(tmp)
tmp = []
tmp.append(item)
if tmp:
res.append(tmp)
print(res)
# [['Title abc', 'text1', 'text'], ['Title def', 'text4', 'text5'], ['Title ghi', 'text4', 'text5']]
答案 3 :(得分:3)
您可以找到“标题”的所有索引,并用它们对列表进行切片
l = ["Title abc", "text1", "text", "Title def", "text4", "text5", "Title ghi", "text4", "text5"]
indices = [i for i, s in enumerate(l) if 'Title' in s]
indices.append(len(l)) # add from last "Title" to the end
new = [l[indices[i]:indices[i+1]] for i in range(len(indices)-1)]
print(new)
# Output: [['Title abc', 'text1', 'text'], ['Title def', 'text4', 'text5'], ['Title ghi', 'text4', 'text5']]
答案 4 :(得分:2)
一种稍微不同的方式,以防您的伴随元素不是恒定的:
student
答案 5 :(得分:1)
更多说明版本
old = ["Title abc", "text1", "text", "Title def", "text4", "text5", "Title ghi", "text4", "text5" ]
new=[]
for i in range(0,len(old),3):
new.append([old[i],old[i+1],old[i+2]])
print(new)
答案 6 :(得分:0)
given_list=["Title abc", "text1", "text", "Title def", "text4", "text5", "Title ghi", "text4", "text5" ]
required_list=[]
for i in range(0,len(given_list),3):
required_list.append(given_list[i:i+3]);
print(required_list)