fork download
  1. # One-liner combinations (minus the import).
  2.  
  3. from itertools import chain
  4.  
  5. combinations = lambda a,k: [()] if k==0 else chain.from_iterable(\
  6. map(lambda i:map(lambda c:tuple(a[i:i+1])+c,combinations(a[i+1:],k-1)),\
  7. range(len(a))))
  8.  
  9. # ..
  10.  
  11. n = 5
  12. a = list(range(1, 1+n))
  13. for k in range(1+n):
  14. print(f'{n} choose {k}')
  15. for x in combinations(a, k):
  16. print(x)
Success #stdin #stdout 0.07s 14124KB
stdin
Standard input is empty
stdout
5 choose 0
()
5 choose 1
(1,)
(2,)
(3,)
(4,)
(5,)
5 choose 2
(1, 2)
(1, 3)
(1, 4)
(1, 5)
(2, 3)
(2, 4)
(2, 5)
(3, 4)
(3, 5)
(4, 5)
5 choose 3
(1, 2, 3)
(1, 2, 4)
(1, 2, 5)
(1, 3, 4)
(1, 3, 5)
(1, 4, 5)
(2, 3, 4)
(2, 3, 5)
(2, 4, 5)
(3, 4, 5)
5 choose 4
(1, 2, 3, 4)
(1, 2, 3, 5)
(1, 2, 4, 5)
(1, 3, 4, 5)
(2, 3, 4, 5)
5 choose 5
(1, 2, 3, 4, 5)