使用itertools.islice()
:
In [246]: characters = [0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
In [247]: it=iter(characters)
In [248]: [list(islice(it,6)) for _ in range(6)]
Out[248]:
[[0, 1, 2, 3, 4, 5],
[6, 7, 8, 9, 'A', 'B'],
['C', 'D', 'E', 'F', 'G', 'H'],
['I', 'J', 'K', 'L', 'M', 'N'],
['O', 'P', 'Q', 'R', 'S', 'T'],
['U', 'V', 'W', 'X', 'Y', 'Z']]
iter(characters)
: 创建characters
列表的迭代器。
islice(iterator,len)
:返回 的迭代器的切片len=6
。Islice 对象本身就是一个迭代器,因此,您需要将islice
对象传递给以list()
获取它的内容。
6
传递给range
可以通过以下方式获得:
In [2]: int(len(characters)/6)
Out[2]: 6