On Fri, Oct 28, 2016 at 7:28 AM, Terry Reedy email@example.com wrote:
s = 'kjskljkxcvnalsfjaweirKJZknzsnlkjsvnskjszsdscccjasfdjf' s2 = ''.join(c for c in s if c in set('abc'))
pretty slick -- but any hope of it being as fast as a C implemented method?
for example, with a 1000 char string:
In : % timeit string.translate(table) 100000 loops, best of 3: 3.62 µs per loop
In : % timeit ''.join(c for c in string if c in set(letters)) 1000 loops, best of 3: 1.14 ms per loop
so the translate() method is about 300 times faster in this case. (and it used a defaultdict with a None factory, which is probably a bit slower than a pure C implementation might be.
I've always figured that Python's rich string methods provided two things:
1) single method call to do common things
2) nice fast, pure C performance
so I think a "keep these" method would help with both of these goals.