gpt4 book ai didi

python - Python 中基于分隔符分割和组合文本

转载 作者:太空宇宙 更新时间:2023-11-03 17:51:45 24 4
gpt4 key购买 nike

我有一个列表列表,其中包含字符串。经过多种正则表达式的工作后,我将要用作分隔符的 @@@ 插入到我的字符串中:

[['@@@this is part one and here is part two and here is part three and heres more and heres more'],
['this is part one@@@and here is part two and here is part three and heres more and heres more'],
['this is part one and here is part two@@@and here is part three and heres more and heres more']
['this is part one and here is part two and here is part three@@@and heres more and heres more']
['this is part one and here is part two and here is part three and heres more@@@and heres more']]

现在,我需要想出这个:

[['this is part one'],['and here is part two'],['and here is part three'], ['and heres more'], ['and heres more']]  

到目前为止,我的尝试都是臃肿、老套且丑陋的。我发现自己在 split 、组合和匹配。谁能推荐一些关于此类问题的一般建议,以及使用哪些工具来使其易于管理?

编辑请注意! and Heres more 确实在理想输出中出现了两次!

最佳答案

我认为您实际上需要抓取 @@@ 之后直到下一个 and 或字符串结尾的所有字符。

>>> [[m] for x in l for m in re.findall(r'@@@(.*?)(?=\sand\b|$)', x[0])]
[['this is part one'], ['and here is part two'], ['and here is part three'], ['and heres more'], ['and heres more']]

关于python - Python 中基于分隔符分割和组合文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29000435/

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