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)
 Return a functor that takes a string and returns a copy of that
        string consisting of only the characters in 'keep'.
    
recipes/Python/59857_Filter_string_only_keep_given_set/recipe-59857.py