Python:单个字符串中重复示例的字符串拆分[已关闭]

xqkwcwgp  于 2023-03-11  发布在  Python
关注(0)|答案(1)|浏览(131)

已关闭。此问题需要超过focused。当前不接受答案。
**想要改进此问题吗?**更新此问题,使其仅关注editing this post的一个问题。

14分钟前就关门了。
Improve this question
如何从字符串中拆分和删除重复的模式,例如?

# sample
s1 = '-c /home/test/pipeline/pipelines/myspace4/.cache/sometexthere/more --log /home/test1/pipeline2/pipelines1/myspace1/.cache/sometexthere/more --arg /home/test4/pipeline3/pipelines3/myspace3/.cache/sometexthere/more --newarg etc.'

# expected
expected = '-c sometexthere/more --log sometexthere/more --arg sometexthere/more --newarg etc.'

# attempt only yields last value rather than all
''.join([ s for s in s1.split('/.cache/')[-1] ])
vql8enpb

vql8enpb1#

首先在空格周围拆分字符串,这样所有相关部分都将在之后拆分:

s2 = ' '.join([part.split('/.cache/')[-1] for part in s1.split()])

输出:

'-c sometexthere/more --log sometexthere/more --arg sometexthere/more --newarg etc.'

相关问题