Sunday, December 6, 2015

Probability: How to do combinations without repetition


Command:
itertools.combinations(iterable, r)

Return r length subsequences of elements from the input iterable.

Combinations are emitted in lexicographic sort order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination.

Example:
from itertools import combinations

print list(combinations([1,2,3,4], 3))

itertools.combinations returns an iterator. An iterator is something that you can for on. So, that is why it is transformed into a list in the example.




No comments:

Post a Comment