Find all combinations of pairings between two lists x and y, such that all elements from y are paired with exactly one from x

Related searches

I am trying to find in python3 each unique combination of elements from two lists x and y, such that for each combination, all elements from list y are paired up with exactly one element from list x (min(x,y) ^ max(x,y) combos). For example, with the following:

x = ['a', 'b', 'c']
y = [1, 2, 3]

combos = get_combos(x,y)
for combo in combos:
    print(combo)

...I want to write get_combos(x, y) such that it returns a list of 27 combos of pairs which, when printed, looks like:

[('a', 1) ('a', 2) ('a', 3)]
[('a', 1) ('a', 2) ('b', 3)]
[('a', 1) ('a', 2) ('c', 3)]
[('a', 1) ('b', 2) ('a', 3)]
[('a', 1) ('b', 2) ('b', 3)]
[('a', 1) ('b', 2) ('c', 3)]
[('a', 1) ('c', 2) ('a', 3)]
[('a', 1) ('c', 2) ('b', 3)]
[('a', 1) ('c', 2) ('c', 3)]
[('b', 1) ('a', 2) ('a', 3)]
[('b', 1) ('a', 2) ('b', 3)]
[('b', 1) ('a', 2) ('c', 3)]
[('b', 1) ('b', 2) ('a', 3)]
[('b', 1) ('b', 2) ('b', 3)]
[('b', 1) ('b', 2) ('c', 3)]
[('b', 1) ('c', 2) ('a', 3)]
[('b', 1) ('c', 2) ('b', 3)]
[('b', 1) ('c', 2) ('c', 3)]
[('c', 1) ('a', 2) ('a', 3)]
[('c', 1) ('a', 2) ('b', 3)]
[('c', 1) ('a', 2) ('c', 3)]
[('c', 1) ('b', 2) ('a', 3)]
[('c', 1) ('b', 2) ('b', 3)]
[('c', 1) ('b', 2) ('c', 3)]
[('c', 1) ('c', 2) ('a', 3)]
[('c', 1) ('c', 2) ('b', 3)]
[('c', 1) ('c', 2) ('c', 3)]

I have looked at itertools.combinations, itertools.product, and itertools.permutations, but none of them seem to give me exactly what I'm looking for. itertools.permutations gets me close when used with zip (see this answer), but there the resulting lists are exclusive in the sense that no element from either list may be repeated in a single combination (e.g. [('a', 1), ('a', 2), ('c', 3)] would be left out), which is not what I want. Will itertools work for this or is this something that needs to be written from scratch?

For me it looks like task for itertools.product and zip, I would do:

import itertools
x = ['a', 'b', 'c']
y = [1, 2, 3]
for t in itertools.product(x,repeat=3):
    print(list(zip(t,y)))

Output:

[('a', 1), ('a', 2), ('a', 3)]
[('a', 1), ('a', 2), ('b', 3)]
[('a', 1), ('a', 2), ('c', 3)]
[('a', 1), ('b', 2), ('a', 3)]
[('a', 1), ('b', 2), ('b', 3)]
[('a', 1), ('b', 2), ('c', 3)]
[('a', 1), ('c', 2), ('a', 3)]
[('a', 1), ('c', 2), ('b', 3)]
[('a', 1), ('c', 2), ('c', 3)]
[('b', 1), ('a', 2), ('a', 3)]
[('b', 1), ('a', 2), ('b', 3)]
[('b', 1), ('a', 2), ('c', 3)]
[('b', 1), ('b', 2), ('a', 3)]
[('b', 1), ('b', 2), ('b', 3)]
[('b', 1), ('b', 2), ('c', 3)]
[('b', 1), ('c', 2), ('a', 3)]
[('b', 1), ('c', 2), ('b', 3)]
[('b', 1), ('c', 2), ('c', 3)]
[('c', 1), ('a', 2), ('a', 3)]
[('c', 1), ('a', 2), ('b', 3)]
[('c', 1), ('a', 2), ('c', 3)]
[('c', 1), ('b', 2), ('a', 3)]
[('c', 1), ('b', 2), ('b', 3)]
[('c', 1), ('b', 2), ('c', 3)]
[('c', 1), ('c', 2), ('a', 3)]
[('c', 1), ('c', 2), ('b', 3)]
[('c', 1), ('c', 2), ('c', 3)]

Note odometer-like nature of itertools.product

Find number of pairs (x, y) in an array such that x^y > y^x , Given two arrays X[] and Y[] of positive integers, find number of pairs such that x^ y > y^x The brute force solution is to consider each element of X[] and Y[], and check All the numbers after idx satisfy the relation so just add (n-idx) to the count. Function to return count of pairs with x as one element. The above formula can help you generate the possible combinations with only two lists, if there are more than two lists values need to be listed the possible combinations, the formula will not work. Here, I can recommend a powerful tool -- Kutools for Excel , it contains a handy feature List All Combinations which can quickly list all the

Basic recursive solution without imports:

x = ['a', 'b', 'c']
y = [1, 2, 3]
def groups(d, c=[]):
  if len(c) == len(x):
    yield list(zip(c, y))
  else:
    for i in d:
       yield from groups(d, c+[i])

print(list(groups(x)))

Output:

[[('a', 1), ('a', 2), ('a', 3)], [('a', 1), ('a', 2), ('b', 3)], [('a', 1), ('a', 2), ('c', 3)], [('a', 1), ('b', 2), ('a', 3)], [('a', 1), ('b', 2), ('b', 3)], [('a', 1), ('b', 2), ('c', 3)], [('a', 1), ('c', 2), ('a', 3)], [('a', 1), ('c', 2), ('b', 3)], [('a', 1), ('c', 2), ('c', 3)], [('b', 1), ('a', 2), ('a', 3)], [('b', 1), ('a', 2), ('b', 3)], [('b', 1), ('a', 2), ('c', 3)], [('b', 1), ('b', 2), ('a', 3)], [('b', 1), ('b', 2), ('b', 3)], [('b', 1), ('b', 2), ('c', 3)], [('b', 1), ('c', 2), ('a', 3)], [('b', 1), ('c', 2), ('b', 3)], [('b', 1), ('c', 2), ('c', 3)], [('c', 1), ('a', 2), ('a', 3)], [('c', 1), ('a', 2), ('b', 3)], [('c', 1), ('a', 2), ('c', 3)], [('c', 1), ('b', 2), ('a', 3)], [('c', 1), ('b', 2), ('b', 3)], [('c', 1), ('b', 2), ('c', 3)], [('c', 1), ('c', 2), ('a', 3)], [('c', 1), ('c', 2), ('b', 3)], [('c', 1), ('c', 2), ('c', 3)]]

Python, Program to find the Nth natural number with exactly two bits set In the 1st step, we find all the combinations of elements using we perform the possible pairing with the result of step 1 using zip() and The original list 2 is : ['x', 'y'] Please Improve this article if you find anything incorrect by clicking on� Excel Formula Training. Formulas are the key to getting things done in Excel. In this accelerated training, you'll learn how to use formulas to manipulate text, work with dates and times, lookup values with VLOOKUP and INDEX & MATCH, count and sum with criteria, dynamically rank values, and create dynamic ranges.

Here is a combination of combinations_with_replacement and permutations that yield the 27.

a = set([])
for i in combinations_with_replacement(['a','b','c'],3):
    for j in permutations(i):
        a.add(j)

assert len(a) == 27

for i in a:
    print(list(zip(i,[1,2,3])))

yields:

[('b', 1), ('b', 2), ('b', 3)]
[('a', 1), ('a', 2), ('c', 3)]
[('b', 1), ('a', 2), ('b', 3)]
[('c', 1), ('a', 2), ('c', 3)]
[('c', 1), ('b', 2), ('a', 3)]
[('c', 1), ('c', 2), ('c', 3)]
[('a', 1), ('c', 2), ('a', 3)]
[('c', 1), ('b', 2), ('c', 3)]
[('c', 1), ('a', 2), ('a', 3)]
[('a', 1), ('a', 2), ('a', 3)]
[('a', 1), ('c', 2), ('b', 3)]
[('a', 1), ('c', 2), ('c', 3)]
[('c', 1), ('c', 2), ('a', 3)]
[('c', 1), ('b', 2), ('b', 3)]
[('a', 1), ('b', 2), ('a', 3)]
[('c', 1), ('c', 2), ('b', 3)]
[('a', 1), ('a', 2), ('b', 3)]
[('b', 1), ('c', 2), ('a', 3)]
[('b', 1), ('b', 2), ('c', 3)]
[('c', 1), ('a', 2), ('b', 3)]
[('b', 1), ('a', 2), ('c', 3)]
[('b', 1), ('c', 2), ('c', 3)]
[('b', 1), ('a', 2), ('a', 3)]
[('a', 1), ('b', 2), ('c', 3)]
[('a', 1), ('b', 2), ('b', 3)]
[('b', 1), ('b', 2), ('a', 3)]
[('b', 1), ('c', 2), ('b', 3)]

How to get all unique combinations of two lists in Python, Combinations of two lists are created by pairing each element from a list with For example, [('a', 1), ('c', 2)] and [('b', 1), ('a', 2)] are two unique combinations of Use itertools.permutations() and zip() to get all unique combinations of two lists machine learning to provide you with code completions in real time sorted by� It has rejected any with a and b, or a and c, or b and c, or even all three a,b and c. So {a,d,e) is allowed (only one out of a,b and c is in that) But {b,c,d} is rejected (it has 2 from the list a,b,c)

Try this

import itertools
L = ['a','b','c']
P = list( itertools.product(L, repeat=3) )
[ [(x,1),(y,2),(z,3)] for x,y,z in P ]

How to generate all possible combinations of a set of numbers in excel, 1 day ago � I need excel formula to generate a list of all possible combinations for Aug 17, 2020 � So in a lotto 6/49 game, picking all your numbers from 1 to 25 N= Y! / (Y - X)! Jul 30, 2011 � how to display all possible different combinations of show you how to quickly get all possible combinations of items from two lists� In regards to 'y' combinations of 'n' objects; is there any quick/easy way for excel to generate and display a list of all possible combination pairings or matchings? NOTE: I don't mean display the total number of possible combinations 'y', but rather, I want to see WHAT all the possible combinations actually are.

Combinations and Permutations, Sample problems with step-by-step solutions show how to use formulas. How many different ways can you select 2 letters from the set of letters: X, Y, and Z? Solution: One way to solve this problem is to list all of the possible selections of 2 You can find the Combination and Permutation Calculator in Stat Trek's main� itertools.combinations() module in Python to print all possible combinations Count of pairs from arrays A and B such that element in A is greater than element in B at that index Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array

Find number of pairs (x, y) in an Array such that x^y > y^x | Set 2; Find the count of even odd pairs in a given Array; Find the sum of all possible pairs in an array of N elements; Find all the intersecting pairs from a given array; Find two non-overlapping pairs having equal sum in an Array; Find k ordered pairs in array with minimum difference d

combos = combntns(set,subset) returns a matrix whose rows are the various combinations that can be taken of the elements of the vector set of length subset. Many combinatorial applications can make use of a vector 1:n for the input set to return generalized, indexed combination subsets.