本文介绍了如何在python族元素由n个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I'd like to get groups of size n elements from a list l:
ie:
[1,2,3,4,5,6,7,8,9] -> [[1,2,3], [4,5,6],[7,8,9]] where n is 3
解决方案
You can use grouper from the recipes on the itertools documentation page:
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
这篇关于如何在python族元素由n个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!