加入列表中两个元素之间的列表元素

发布于 2025-01-31 09:50:34 字数 757 浏览 2 评论 0原文

我有一个列表,我有两个列表元素开始:结束:。在这两个之间,我想加入start元素的不确定元素。 另外,结束:可以具有不同的名称,但它总是以结束:开始。这是我的列表

sth_list = ['name: michael', 'age:56', 'start:','','something','is','happening','end:', 'anything could be here:', 'some other things:', 'more things:'] 

,我想得到

 ['name: michael', 'age:56', 'start: something is happening', 'end:', 'anything could be here:', 'some other things:', 'more things:']

我到目前为止所拥有的。但这只给了我在开始和结束之间的连接元素,但我想拥有上面的完整列表。

 ''.join([element for n, element in enumerate(sth_list) if ((n>sth_list.index("start:")) & (n<sth_list.index([e for e in sth_list if e.startswith('end')][0])))])

I have a list and I have the two list elements start:and end:. Between these two there is an undefined number of elements that I would like to join to the start element.
Also, end: can have different names but it always starts with end:. This is my list

sth_list = ['name: michael', 'age:56', 'start:','','something','is','happening','end:', 'anything could be here:', 'some other things:', 'more things:'] 

and I would like to get this

 ['name: michael', 'age:56', 'start: something is happening', 'end:', 'anything could be here:', 'some other things:', 'more things:']

what I have so far is this. But it only gives me the joined elements between start and end but I would like to have the full list as above.

 ''.join([element for n, element in enumerate(sth_list) if ((n>sth_list.index("start:")) & (n<sth_list.index([e for e in sth_list if e.startswith('end')][0])))])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心舞飞扬 2025-02-07 09:50:34

获取开始的索引:结束:。然后,您可以加入它们并使用切片分配将列表的该部分替换为结果。

start = sth_list.index('start:')
end = min(i for i, s in enumerate(sth_list) if s.startswith('end:'))
sth_list[start:end] = [' '.join(sth_list[start:end])]

请注意,仅当只有一个start:/end:配对时,这才有效。

Get the indexes of start: and end:. Then you can join them and use slice assignment to replace that part of the list with the result.

start = sth_list.index('start:')
end = min(i for i, s in enumerate(sth_list) if s.startswith('end:'))
sth_list[start:end] = [' '.join(sth_list[start:end])]

Note that this only works if there's just one start:/end: pair in the list.

忆依然 2025-02-07 09:50:34

您可以使用经典循环:

import re

out = []
flag = False
for item in sth_list:
    if item.startswith('end:'):
        flag = False
    if flag:
        if item:
            out[-1] += ' '+item
    else:
        out.append(item)
    if item == 'start:':
        flag = True

输出:

['name: michael',
 'age:56',
 'start: something is happening',
 'end:',
 'anything could be here:',
 'some other things:',
 'more things:']

You can use a classical loop:

import re

out = []
flag = False
for item in sth_list:
    if item.startswith('end:'):
        flag = False
    if flag:
        if item:
            out[-1] += ' '+item
    else:
        out.append(item)
    if item == 'start:':
        flag = True

output:

['name: michael',
 'age:56',
 'start: something is happening',
 'end:',
 'anything could be here:',
 'some other things:',
 'more things:']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文