Reputation: 559
I'm trying to figure out how to delete everything from a string except the square brackets
If a user enters "Hello, my name is [lol] bob [not[really]] john[son]"
or enters "[[[[]]][[][][]"
If the user enters something like the first statement I want to delete everything except the brackets. I do not want to implement input validation. Cheers
Edit: Thanks for the help guys I love you
Upvotes: 2
Views: 137
Reputation: 95622
When in doubt about the answer it is worth trying a few and timing them:
PS C:\python33> .\python.exe lib\timeit.py -s @"
s = 'Hello, my name is [lol] bob [not[really]] john[son]'
"@ @"
for x in frozenset(s)-frozenset('[]'):
s = s.replace(x, '')
"@
1000000 loops, best of 3: 0.844 usec per loop
PS C:\python33> .\python.exe lib\timeit.py -s @"
s = 'Hello, my name is [lol] bob [not[really]] john[son]'
"@ "''.join(c for c in s if c in '[]')"
100000 loops, best of 3: 4.79 usec per loop
PS C:\python33> .\python.exe lib\timeit.py -s @"
s = 'Hello, my name is [lol] bob [not[really]] john[son]'
"@ @"
s.translate(str.maketrans('','',''.join(set(s)-set('[]'))))
"@
100000 loops, best of 3: 8.5 usec per loop
PS C:\python33> .\python.exe lib\timeit.py -s @"
s = 'Hello, my name is [lol] bob [not[really]] john[son]'
"@ "''.join(filter(lambda x: x in '[]', s))"
100000 loops, best of 3: 9.67 usec per loop
PS C:\python33> .\python.exe lib\timeit.py -s @"
s = 'Hello, my name is [lol] bob [not[really]] john[son]'
import re
"@ "re.sub('[^][]', '', s)"
100000 loops, best of 3: 15.9 usec per loop
So @guess's now deleted answer using s.replace()
over the set of characters to remove seems to be fastest and regular expression substitution the slowest (but that's Python 3.3 and my machine, others may differ).
Also note that if you replace the setup string in all of the above with:
s = 'Hello, my name is [lol] bob [not[really]] john[son]'*1000
Then the times I get are:
10000 loops, best of 3: 190 usec per loop
100 loops, best of 3: 3.6 msec per loop
100 loops, best of 3: 4.25 msec per loop
100 loops, best of 3: 8.72 msec per loop
100 loops, best of 3: 7.13 msec per loop
So re.sub
scales better than the lambda
solution, but otherwise the others stay in the same relative positions.
Upvotes: 1
Reputation: 20351
Using lambda
and filter
- just another way to do it, but I find it more beautiful.
>>> your_input = "Hello, my name is [lol] bob [not[really]] john[son]"
>>> ''.join(filter(lambda x: x in '[]', your_input))
'[][[]][]'
Upvotes: 2
Reputation: 34146
You can use join()
:
s = "Hello, my name is [lol] bob [not[really]] john[son]"
s = ''.join(c for c in s if c in '[]')
print s
Output:
[][[]][]
Upvotes: 6