Skip to content

Instantly share code, notes, and snippets.

@Pentusha
Created October 29, 2015 14:01
Show Gist options
  • Select an option

  • Save Pentusha/ed4691573fb4baec07dd to your computer and use it in GitHub Desktop.

Select an option

Save Pentusha/ed4691573fb4baec07dd to your computer and use it in GitHub Desktop.
chunk_numeration.py
from itertools import zip_longest
from pprint import pprint
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
def chunks(iterable, n, fill_value=None):
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fill_value)
c1 = list(chunks(a, 3))
c2 = list(chunks(enumerate(a), 3))
c3 = list(enumerate(chunks(a, 3)))
c4 = list(enumerate(chunks(enumerate(a), 3)))
assert c1 == [
(1, 2, 3),
(4, 5, 6),
(7, 8, 9),
(0, None, None),
], 'Чанки по 3'
assert c2 == [
((0, 1), (1, 2), (2, 3)),
((3, 4), (4, 5), (5, 6)),
((6, 7), (7, 8), (8, 9)),
((9, 0), None, None),
], 'Чанки по 3 сквозная нумерация'
assert c3 == [
(0, (1, 2, 3)),
(1, (4, 5, 6)),
(2, (7, 8, 9)),
(3, (0, None, None))
], 'Чанки по 3 нумерация внутри чанков'
assert c4 == [
(0, ((0, 1), (1, 2), (2, 3))),
(1, ((3, 4), (4, 5), (5, 6))),
(2, ((6, 7), (7, 8), (8, 9))),
(3, ((9, 0), None, None)),
], 'Чанки по 3 сквозная нумерация и нумерация внутри чанков'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment