We can use combinations function from itertools module to create combinations:
#ex1a.py from itertools import combinations A = [1,2,3] B = list(combinations(A,2)) print 'The list is ', B, ' with a length of ', len(B) ## The list is [(1, 2), (1, 3), (2, 3)] with a length of 3
We can also create a function using the factorials function
# ex1b.py
from math import factorial
def choose1(n,k):
return factorial(n)/factorial(k)/factorial(n-k)
print choose1(n=3,k=2)
print choose1(3,2)
## 3
## 3
No comments:
Post a Comment