[Python-Dev] str.strip() enhancement (original) (raw)
Jonny Reichwald kalinda at acc.umu.se
Sat Sep 3 22:36:07 CEST 2005
- Previous message: [Python-Dev] Replacement for print in Python 3.0
- Next message: [Python-Dev] str.strip() enhancement
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi,
I would like to suggest a small enhancement to str.strip().
By expanding its current form, where it only takes a char list, to
taking any list containing either char lists or string lists, it is
possible to remove entire words from the stripped string.
To clarify what I mean, here are some examples, first argument string
to be stripped, second argument a list of things to strip:
#A char list gives the same result as the standard strip
my_strip("abcdeed", "de") 'abc'
#A list of strings instead
my_strip("abcdeed", ("ed",)) 'abcde'
#The char order in the strings to be stripped are of importance
my_strip("abcdeed", ("ad", "eb")) 'abcdeed'
Functions used in the above examples:
def my_lstrip(str, list): ret_str = str[max([k == True and len(v) for (k,v) in zip ([str.startswith(e) for e in list], list)]):] if ret_str != str: return my_lstrip(ret_str, list) return str
def my_rstrip(str, list):
ret_str = str[:len(str)-max([k == True and len(v) for (k,v)
in zip([str.endswith(e) for e in list], list)])]
if ret_str != str and ret_str != False:
return my_rstrip(ret_str, list)
return str
def my_strip(str, list): return my_lstrip(my_rstrip(str, list), list)
Would this be useful for anyone else besides me?
-- Jonny Reichwald
- Previous message: [Python-Dev] Replacement for print in Python 3.0
- Next message: [Python-Dev] str.strip() enhancement
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]