For example, if the set is {0, 1, 2, 3}
, then the different ways to choose two elements is: {0,1}, {0,2}, {0,3}, {1,2}, {1,3}, {2,3}
. So !4$ = 6
.
Asked
Active
Viewed 37 times
-3

Rory McCrossan
- 331,213
- 40
- 305
- 339

Ariel
- 1
-
3What is n and why do you divide it by 2? – Kelly Bundy Sep 22 '22 at 15:09
-
Does this answer your question? [Efficient computation of n choose k in Node.js](https://stackoverflow.com/questions/37679987/efficient-computation-of-n-choose-k-in-node-js) – Konrad Sep 22 '22 at 15:13
1 Answers
1
I think you are looking for itertools.combinations
import itertools
elts = {1, 2, 3, 4}
result = [i for i in itertools.combinations(elts, 2)]
print (result) #[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

mcgusty
- 1,354
- 15
- 21