我有以下代码删除了“引用/更改/数字”,但没有得到预期的输出,我有如下所示的当前输出,任何人都可以提供输入
如何解决这个问题?
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/