combinations




Pay Notebook Creator: Salah Ahmed0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0

Subsets in python: different ways to pair

In looking for ways to make couplets of items I've discovered four brilliant methods in the itertools module.

In making couplets its important to understand how the pairing should be defined. Once an item is chosen, can it be chosen again as the next element in the group? If a set is defined as {0, 1, 2, 3}, can we define a group as (0, 0)? This is referred to as replacement.

In making these groups, does the order of the elements matter? is (0, 1) unique to (1, 0)? If it doesn't matter than my set of groups should only include one of the pair, but if it does the set should include both. A good analogy to make is if you were choosing a leader for each pair, the leader being the first in the pair, then the order would matter—0 is the leader in (0, 1) and vice versa—versus if you were just choosing teams where each member held the same status; it wouldn't matter in what order you defined the group they're all the same group.

The itertools methods are the following

  • itertools.product
  • itertools.combinations
  • itertools.combinations_with_replacement
  • itertools.permutations
In [3]:
from itertools import (
    product, combinations, 
    combinations_with_replacement, permutations)
In [6]:
l = list(range(3))
npairs = 2
In [3]:
print(l)
[0, 1, 2]

Permutations

  • WITHOUT replacement, so once an item is taken, only other items can be used
  • Order DOES matter so (1, 0) is different from (0, 1)
In [4]:
list(permutations(l, npairs))
Out[4]:
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]

Combinations

  • WITHOUT replacement, so once an item is taken, only other items can be used
  • Order DOESN'T matter, so (1, 0) is not unique to (0, 1)

This is essentially the different ways to order a pair measured by the Binomial Coefficient

In [5]:
list(combinations(l, npairs))
Out[5]:
[(0, 1), (0, 2), (1, 2)]

Combinations w/ replacement

  • WITH replacement, so an item can be used again in same pair
  • Order DOESN'T matter, so (1, 0) is not unique to (0, 1)
In [7]:
list(combinations_with_replacement(l, npairs))
Out[7]:
[(0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 2)]

Product

product operates on multiple lists, returning pairings between items of different lists. To use on the same list (you probably shouldn't) you would specify the size of the group with the parameter keyword repeat

IF used in this way one think of it as WITH replacement, where order DOES matter, so (0, 0) is acceptable, and (1,0) is unique to (0, 1)

In [7]:
list(product(l, repeat=npairs))
Out[7]:
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

A better way to use product is with different lists

In [4]:
shirts = ['hawaii', 'collared']
shoes = ['sneakers', 'slippers']
pants = ['jeans', 'shorts']

outfits = list(product(shirts, shoes, pants))
In [5]:
outfits
Out[5]:
[('hawaii', 'sneakers', 'jeans'),
 ('hawaii', 'sneakers', 'shorts'),
 ('hawaii', 'slippers', 'jeans'),
 ('hawaii', 'slippers', 'shorts'),
 ('collared', 'sneakers', 'jeans'),
 ('collared', 'sneakers', 'shorts'),
 ('collared', 'slippers', 'jeans'),
 ('collared', 'slippers', 'shorts')]
Method With Replacement Order Matters
permutations X
combinations
combinations_with_replacement X
product X X