除了itertools之外,还有更多在iterables上操作的例程
more-itertools的Python项目详细描述
开始
要开始使用,请使用pip安装库
pip install more-itertools
itertools docs中的菜谱 包含在顶级套餐中:
>>>frommore_itertoolsimportflatten>>>iterable=[(0,1),(2,3)]>>>list(flatten(iterable))[0,1,2,3]
还提供了几种新配方:
>>>frommore_itertoolsimportchunked>>>iterable=[0,1,2,3,4,5,6,7,8]>>>list(chunked(iterable,3))[[0,1,2],[3,4,5],[6,7,8]]>>>frommore_itertoolsimportspy>>>iterable=(x*xforxinrange(1,6))>>>head,iterable=spy(iterable,n=3)>>>list(head)[1,4,9]>>>list(iterable)[1,4,9,16,25]
有关函数的完整列表,请参阅API文档