在 Python 中使用,和类似的可迭代对象实现类似str.strip()
行为的方法是什么?list
tuple
例子:
str.strip()
-喜欢
>>> lst = ['\t', 0, 'a', ' ', 0, '\n', '\n', '', '\t']
>>> list_strip(lst)
... [0, 'a', ' ', 0]
>>> list_strip(lst, elements=(0, '\n', '', ' ', '\t'))
... ['a']
str.lstrip()
-喜欢
>>> lst = ['\t', 0, 'a', ' ', 0, '\n', '\n', '', '\t']
>>> list_lstrip(lst)
... [0, 'a', ' ', 0, '\n', '\n', '', '\t']
>>> list_lstrip(lst, elements=(0, '\n', '', ' ', '\t'))
... ['a', ' ', 0, '\n', '\n', '', '\t']
str.rstrip()
-喜欢
>>> lst = ['\t', 0, 'a', ' ', 0, '\n', '\n', '', '\t']
>>> list_rstrip(lst)
... ['\t', 0, 'a', ' ', 0]
>>> list_rstrip(lst, elements=(0, '\n', '', ' ', '\t'))
... ['\t', 0, 'a']
实现功能的原型如下
def set_force_iterable(val):
if type(val) is str:
return [val, ]
else:
try:
iter(val)
return val
except TypeError:
return [val, ]
def list_rstrip(lst, elements=None):
'''Return a *copy* of the list or new tuple with trailing whitespace removed.
Like :func:`str.rsrtip`.
Parameters
----------
lst : list or tuple
List or tuple to be stripped.
elements : iterable or None (default None)
Elements to be stripped. Default None: strip all whitespaces.
Returns
-------
list or tuple
Return a *copy* of the list or new tuple with trailing whitespace removed.
If elements is given and not None, remove values in elements instead.
Examples
--------
>>> lst = ['\t', 0, 'a', ' ', 0, '\n', '\n', '', '\t']
>>> list_rstrip(lst)
... ['\t', 0, 'a', ' ', 0]
>>> list_rstrip(lst, elements=(0, '\n', '', ' ', '\t'))
... ['\t', 0, 'a']
'''
assert isinstance(lst, list) or isinstance(lst, tuple), '`lst` is not list or tuple'
if elements is None:
elements = ("", " ", "\t", "\n")
else:
elements = set_force_iterable(elements)
if len(lst) == 0 or (len(lst) == 1 and lst[0] in elements):
if isinstance(lst, list):
return []
else:
return ()
else:
if lst[-1] not in elements:
if isinstance(lst, list):
return lst.copy()
else:
return lst
prev_will_removed = True
for i, el in enumerate(reversed(lst)):
if not prev_will_removed or el not in elements:
break
return lst[:-i]
def list_lstrip(lst, elements=None):
'''Return a *copy* of the list or new tuple with leading whitespace removed.
Like :func:`str.lsrtip`.
Parameters
----------
lst : list or tuple
List or tuple to be stripped.
elements : iterable or None (default None)
Elements to be stripped. Default None: strip all whitespaces.
Returns
-------
list or tuple
Return a *copy* of the list or new tuple with leading whitespace removed.
If elements is given and not None, remove values in elements instead.
Examples
--------
>>> lst = ['\t', 0, 'a', ' ', 0, '\n', '\n', '', '\t']
>>> list_lstrip(lst)
... [0, 'a', ' ', 0, '\n', '\n', '', '\t']
>>> list_lstrip(lst, elements=(0, '\n', '', ' ', '\t'))
... ['a', ' ', 0, '\n', '\n', '', '\t']
'''
assert isinstance(lst, list) or isinstance(lst, tuple), '`lst` is not list or tuple'
if elements is None:
elements = ("", " ", "\t", "\n")
else:
elements = set_force_iterable(elements)
if len(lst) == 0 or (len(lst) == 1 and lst[0] in elements):
if isinstance(lst, list):
return []
else:
return ()
else:
if lst[0] not in elements:
if isinstance(lst, list):
return lst.copy()
else:
return lst
prev_will_removed = True
for i, el in enumerate(lst):
if not prev_will_removed or el not in elements:
break
return lst[i:]
def list_strip(lst, elements=None):
'''Return a **copy** of the list or new tuple with leading and trailing whitespace removed.
Like :func:`str.srtip`.
Parameters
----------
lst : list or tuple
List or tuple to be stripped.
elements : iterable or None (default None)
Elements to be stripped. Default None: strip all whitespaces.
Returns
-------
list or tuple
Return a **copy** of the list or new tuple with leading and trailing whitespace removed.
If elements is given and not None, remove values in elements instead.
Examples
--------
>>> lst = ['\t', 0, 'a', ' ', 0, '\n', '\n', '', '\t']
>>> list_strip(lst)
... [0, 'a', ' ', 0]
>>> list_strip(lst, elements=(0, '\n', '', ' ', '\t'))
... ['a']
'''
assert isinstance(lst, list) or isinstance(lst, tuple), '`lst` is not list or tuple'
if elements is None:
elements = ("", " ", "\t", "\n")
else:
elements = set_force_iterable(elements)
if len(lst) == 0 or (len(lst) == 1 and lst[0] in elements):
if isinstance(lst, list):
return []
else:
return ()
else:
return list_lstrip(list_rstrip(lst, elements=elements), elements=elements)