gpt4 book ai didi

python - 如何根据空间而不是换行进行分割?

转载 作者:行者123 更新时间:2023-11-30 23:18:00 25 4
gpt4 key购买 nike

我有以下代码,它删除了“refs/changes/digit”,但没有得到预期的输出,我的当前输出如下所示,任何人都可以提供输入如何解决这个问题?

source="refs/changes/44/1025744/3 refs/changes/62/1025962/5 refs/changes/45/913745/2"
output = ' '.join(line.split('/', 3)[-1] for line in source.splitlines())
print output


CURRENT OUTPUT:-

1025744/3 refs/changes/62/1025962/5 refs/changes/45/913745/2

expected output

1025744/3 1025962/5 913745/2

最佳答案

按空格拆分,然后按前三个 "/" 拆分并提取最后一个元素。

source="refs/changes/44/1025744/3 refs/changes/62/1025962/5 refs/changes/45/913745/2"
print(" ".join([ele.split("/",3)[-1] for ele in source.split()]))
1025744/3 1025962/5 913745/2

一些时间安排:

In [3]: import re

In [4]: timeit " ".join([ele.split("/",3)[-1] for ele in source.split()])
100000 loops, best of 3: 1.83 µs per loop

In [5]: timeit p.findall(source)

100000 loops, best of 3: 2.84 µs per loop

关于python - 如何根据空间而不是换行进行分割?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27006481/

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