Consider the following list:
elems = ('banana', 'apple', 'orange', 'melon')
I need to define a function that assigns a unique integer to each ordered combination of one to three non-repeated elements. For example:
banana --> 1
banana, apple --> 2
banana, orange --> 3
banana, melon --> 4
banana, apple, orange --> 5
banana, apple, melon --> 6
banana, orange, apple --> 7
banana, orange, melon --> 8
banana, melon, apple --> 9
banana, melon, orange --> 10
apple --> 11
apple, banana --> 12
apple, orange --> 13
...
My actual list contains more than 20 elements so defining the entire set of combinations manually is not possible. The function would take as input a minimum of one and a maximum of three elements, and return the unique integer associated with them, following the above convention.
I've been playing around with itertools.combinations()
and itertools.combinations_with_replacement()
but they don't return what I need.