Skip to content

Instantly share code, notes, and snippets.

@dkuebric
Created October 23, 2012 06:43
Show Gist options
  • Save dkuebric/3937272 to your computer and use it in GitHub Desktop.
Save dkuebric/3937272 to your computer and use it in GitHub Desktop.
iterate over n choose k
(venv)dan@dev1:~/$ python
Python 2.7.2 (default, Apr 20 2012, 16:27:51)
[GCC 4.6.1] on linux3
Type "help", "copyright", "credits" or "license" for more information.
>>> import itertools
>>> s = [1,2,3,4,5,6]
>>> combos = itertools.combinations(s, 3)
>>> for c in combos
... print c
...
(1, 2, 3)
(1, 2, 4)
(1, 2, 5)
(1, 2, 6)
(1, 3, 4)
(1, 3, 5)
(1, 3, 6)
(1, 4, 5)
(1, 4, 6)
(1, 5, 6)
(2, 3, 4)
(2, 3, 5)
(2, 3, 6)
(2, 4, 5)
(2, 4, 6)
(2, 5, 6)
(3, 4, 5)
(3, 4, 6)
(3, 5, 6)
(4, 5, 6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment