Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- In [106]: def func1(mystr,sub,k):
- inds = (i for i,c in enumerate(reversed(mystr)) if c==sub)
- return len(mystr)-tuple(islice(inds,k-1,k))[-1]-1
- .....:
- In [107]: def func2(mystr,sub,k):
- return [i for i,c in enumerate(mystr) if c==sub][-k]
- .....:
- In [108]: strs="abcdabababcebc"*10000
- In [109]: %timeit func1(strs,'b',200)
- 10000 loops, best of 3: 129 us per loop
- In [110]: %timeit func2(strs,'b',200)
- 10 loops, best of 3: 28.4 ms per loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement