问题描述
通过Python中的str.strip()
,list
和类似的可迭代对象,实现tuple
类行为的方式是什么?
示例:
str.strip()
之类的
>>> lst = ['\t','a',' ','\n','','\t']
>>> list_strip(lst)
... [0,0]
>>> list_strip(lst,elements=(0,'\t'))
... ['a']
str.lstrip()
之类的
>>> lst = ['\t','\t']
>>> list_lstrip(lst)
... [0,'\t']
>>> list_lstrip(lst,'\t'))
... ['a','\t']
str.rstrip()
之类的
>>> lst = ['\t','\t']
>>> list_rstrip(lst)
... ['\t',0]
>>> list_rstrip(lst,'\t'))
... ['\t','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','\t']
>>> list_rstrip(lst)
... ['\t',0]
>>> list_rstrip(lst,'\t'))
... ['\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:
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,'\t']
>>> list_lstrip(lst)
... [0,'\t']
>>> list_lstrip(lst,'\t'))
... ['a','\t']
'''
assert isinstance(lst,'`lst` is not list or tuple'
if elements is None:
elements = ("",list):
return []
else:
return ()
else:
if lst[0] not in elements:
if isinstance(lst,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,'\t']
>>> list_strip(lst)
... [0,0]
>>> list_strip(lst,'\t'))
... ['a']
'''
assert isinstance(lst,list):
return []
else:
return ()
else:
return list_lstrip(list_rstrip(lst,elements=elements),elements=elements)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)