Welcome, guest | Sign In | My Account | Store | Cart

The following code creates a filtering functor for a given character set, that can then be used on any number of strings.

Python, 16 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def makefilter(keep):
    """ Return a functor that takes a string and returns a copy of that
        string consisting of only the characters in 'keep'.
    """
    import string

    # make a string of all chars, and one of all those NOT in 'keep'
    allchars = string.maketrans('', '')
    delchars = ''.join([c for c in allchars if c not in keep])

    # return the functor
    return lambda s,a=allchars,d=delchars: s.translate(a, d)

import string
identifier = makefilter(string.letters + string.digits + '_')
print identifier(string.maketrans('', ''))

Splitting the task into a preparation phase and an execution part drastically improves runtime efficiency. First, we build the needed strings and store them in a functor. Then, we can use the functor to actually filter some strings.

Another important thing to note is that the functor uses the built-in translate() function and is thus very fast, after the two helper strings are created, which happens only once.

2 comments

Nick Perkins 22 years, 10 months ago  # | flag

If it won't fit in a lambda... Well done.

Of course, you don't have to use lambda to create a function which can be returned. You can use any kind of function. If you can't define the new function in a lambda expression, you can just define the function in the 'normal' way, and return it:

ie. the example works just as well if you change:

return lambda s,a=allchars,d=delchars: s.translate(a,d)

to:

def filter_function(s,a=allchars,d=delchars):
    return s.translate(a, d)
return filter_function

In this case it's longer, but lambda is very limited, so it's useful to know that you can use a full function definition.

Phillip Ruggera 19 years, 4 months ago  # | flag

Try a list comprehension.

str = 'USD 890,009.90 '

set = '0123456789,.'

''.join([c for c in str if c in set])

result: '890,009.90'