pythonpython-itertools

I need a universal function for code in python, similar to permutation/combination, have code example


Here's the code example:

res = []
for i in game.keys():
    for j in game.keys():
        if f'{i}{j}' not in res and f'{j}{i}' not in res:
            res.append(f'{i}{j}')
print(res)

I need this, but it should work for not only 2, but 3, 4, 5, 6 and 7 arguments (I wonder if there's a function that returns this and takes arguments like permutation and combination, so function(table_Of_Arguments, number_Of_Elements)). I could write my own function in the above way, but I think there should already be something like that.

I've done some research and many people recommend permutation or combination from itertools, but permutation returns too much (it returns both {i}{j} and {j}{i}) and combination returns too little (it doesn't return cases where i==j). I will be using it with table_Of_Arguments of size 32, so I hope there is something more optimized than my code, because 32^7 is a pretty high number.


Solution

  • You want itertools.combinations_with_replacement