Skip to content

Instantly share code, notes, and snippets.

@hideaki-t
Created March 7, 2012 18:03
Show Gist options
  • Save hideaki-t/1994723 to your computer and use it in GitHub Desktop.
Save hideaki-t/1994723 to your computer and use it in GitHub Desktop.
n-tuple
from itertools import izip, tee
def ntuple(it, n):
its = tee(it, n)
for c, i in enumerate(its):
for j in range(c): i.next()
return izip(*its)
In [5]: for i in f('123456789', 1): print i
('1',)
('2',)
('3',)
('4',)
('5',)
('6',)
('7',)
('8',)
('9',)
In [6]: for i in f('123456789', 2): print i
('1', '2')
('2', '3')
('3', '4')
('4', '5')
('5', '6')
('6', '7')
('7', '8')
('8', '9')
In [7]: for i in f('123456789', 3): print i
('1', '2', '3')
('2', '3', '4')
('3', '4', '5')
('4', '5', '6')
('5', '6', '7')
('6', '7', '8')
('7', '8', '9')
In [8]: for i in f('123456789', 5): print i
('1', '2', '3', '4', '5')
('2', '3', '4', '5', '6')
('3', '4', '5', '6', '7')
('4', '5', '6', '7', '8')
('5', '6', '7', '8', '9')
In [9]: for i in f('123456789', 9): print i
('1', '2', '3', '4', '5', '6', '7', '8', '9')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment