Q:

merge two sets with considering the order of each one (in Python)

I have two variables i, j that they show the length of two sets one and two, like len(one)=i and len(two)=j. Now, I want to merge these two sets such that having the ordered permutations of each set. I need also to index each new set in Python.
for example:
one includes the first i upper case letters, and two includes lowercase letters
 len(one) = i  
    len(two) = j 
expected outputs = {'abABC...', 'aAbBC...', 'aABcC...', 'aABCb...',...}

I have tried the following code but it does not work. I will be thankful if anyone can help.
    from functools import reduce
    from itertools import combinations

    def assign(v, p):
        v[p[0]] = p[1]
        return v

    def interp(word1, word2, size):
        return (''.join(reduce(assign, zip(comb1, word1), zip(comb2, word2)))
                for comb1, comb2 in zip(combinations(range(size), len(word1)),
                                        combinations(range(size), len(word2))))

    print('\n'.join(interp("ABC", "ab", 5)))

A:

You can use a function that merges the first item of one of the two lists with the combinations from the rest of the list recursively:
def merge(a, b):
    if a and b:
        for (first, *rest), other in (a, b), (b, a):
            yield from ([first, *merged] for merged in merge(rest, other))
    elif a or b:
        yield a or b

so that:
for combination in merge(['A', 'B', 'C'], ['a', 'b']):
    print(''.join(combination))

outputs:
ABCab
ABabC
ABaCb
AabBC
AaBCb
AaBbC
abABC
aABCb
aABbC
aAbBC

Note that sets are unordered in Python, so there is no way you can preserve the order of ABC and ab as suggested by your expected output if your input are sets. The example given here assumes your input and output to be lists instead.

