Let me explain the 4 combinatorial iterators from the module `itertools` with ice cream:
1. combinations
2. combinations_with_replacement
3. permutations
4. product
Let me explain the 4 combinatorial iterators from the module `itertools` with ice cream:
1. combinations
2. combinations_with_replacement
3. permutations
4. product
1. `combinations`
This iterator will produce tuples of length `r` with all the unique combinations of values from `iterable`.
(โUniqueโ will make use of the original position in `iterable`, and not the value itself.)
E.g., what ice cream flavour combinations can I get?
2. `combinations_with_replacement`
Same as `combinations`, but values can be repeated.
E.g., what ice cream flavour combinations can I get if I allow myself to repeat flavours?
3. `permutations`
All possible combinations of size `r` in all their possible orderings.
E.g., if I get 2 scoops, how can they be served?
This is a very important question because the flavour at the bottom is eaten last!
4. `product`
Matches up all values of all iterables together.
(Computes the cartesian product of the given iterables.)
E.g., if I can get either 2 or 3 scoops, and if the ice cream can be served on a cup or on a cone, how many different orders are there?
These are the 4 combinatorial iterators from the module `itertools`.
The module itertools contains a total of 19 (!) iterators!
If you want to learn more about them, take a look at this short article I wrote yesterday: