Reputation: 33
I have the following code which removes the "refs/changes/digit" but am not getting the expected output,i have the curent output as shown below,can anyone provide inputs on how to fix this?
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
Upvotes: 1
Views: 65
Reputation: 180401
split on whitespace then split on the first three "/"
and extract the last element.
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
some timings:
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
Upvotes: 2
Reputation: 24324
Another approach would be to use regular expression:
import re
p = re.compile(r'(\d{6,7}/\d{1})')
source="refs/changes/44/1025744/3 refs/changes/62/1025962/5 refs/changes/45/913745/2"
print p.findall(source)
Note that I explicitly define number before slash to be 6 or 7 characters long (you can adjust regexp to your needs).
Upvotes: 1