I need to create a list of all the permutation but excluding that ones where there is the same number changed of sign.
For example, from the sequence
[-2, -1, 1, 2]
I would obtain all permutations like these:
[-2, -1], [-2, 1], [-1, -2], [-1, 2], [1, -2], [1, 2], [2, -1], [2, 1]
At the moment I use the following code:
permutation_items = []
permutations = itertools.permutations(range_items, items)
permutation_item = list(permutations)
where for example range_items = [-2, -1, 1, 2]
and items = 2
Then to eliminate all opposite duplicates I use
for element in permutation_items:
flag=0
for j in element:
if ((j in element) & ((j*-1) in element)):
flag = 1
break
if flag == 0:
all_solutions.append(element)
I think this is not the best way because first I create a list with all permutations then I delete those I don't want, could you suggest a better way? Also because if I need to create a list of permutations with 10 or more numbers it becomes very big...
Do you think I'll have some problems with these dimensions?
Please note: with these permutations I need to do further operations (I need to find the minimum number of permutations that give all possible couples of numbers), so I think I need to store them in a variable, also because at the end of my algorithm I need to store results in a file.
...ok guys, your answer are very good and I like your interest...now, if I use for my variable 'range_items' a list of 30 elements (positives and negatives) the time used by the code is very big, I am thinking to ask you for a multithread solution (so I can load the code in a cluster with many cores)...is it feasible?