python flatten sequences,flattensequences,A simple met
python flatten sequences,flattensequences,A simple met
A simple method to flatten a sequence (list, tuple...)
def flatten(list): """Flatten a list of elements into a unique list Author: Christophe Simonis Examples: >>> flatten(['a']) ['a'] >>> flatten('b') ['b'] >>> flatten( [] ) [] >>> flatten( [[], [[]]] ) [] >>> flatten( [[['a','b'], 'c'], 'd', ['e', [], 'f']] ) ['a', 'b', 'c', 'd', 'e', 'f'] >>> t = (1,2,(3,), [4, 5, [6, [7], (8, 9), ([10, 11, (12, 13)]), [14, [], (15,)], []]]) >>> flatten(t) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] """ def isiterable(x): return hasattr(x, "__iter__") r = [] for e in list: if isiterable(e): map(r.append, flatten(e)) else: r.append(e) return rif __name__ == '__main__': import doctest doctest.testmod()
相关内容
- python生成zip压缩文件目录结构,,下面python代码片段
- python里的签名方法hmac,python签名hmac,>>> import h
- 正则表达式断言学习,正则表达式断言,关于前瞻断言,
- import python 配置文件,importpython,这段代码演示使用imp
- Python 获取目录下的文件列表与内容,python获取,下面的
- Python使用socket类获取服务器IP地址,pythonsocket,Python 获取
- python源代码语法高亮输出,python源代码高亮,## {{{ http
- unix subprocess wrapper,subprocesswrapper,I have used
- Python+MongDB实现短链接,pythonmongdb实现,实现任意数制变
- Automatic ref-count management in C++ using a smart ptr,ref-countpt
评论关闭