You can use itertools.product
and permutations
and zip
:
>>> from itertools import product ,permutations
>>> p=list(permutations(['2','3','4']))
>>> l=[('-'+i,'-'+j,'-'+k) for i,j,k in permutations(['2','3','4'])]
>>> new=zip(l,p)
>>> new
[(('-2', '-3', '-4'), ('2', '3', '4')), (('-2', '-4', '-3'), ('2', '4', '3')), (('-3', '-2', '-4'), ('3', '2', '4')), (('-3', '-4', '-2'), ('3', '4', '2')), (('-4', '-2', '-3'), ('4', '2', '3')), (('-4', '-3', '-2'), ('4', '3', '2'))]
>>> list(list(product(*zip(j,i))) for i,j in new)
[[('2', '3', '4'), ('2', '3', '-4'), ('2', '-3', '4'), ('2', '-3', '-4'), ('-2', '3', '4'), ('-2', '3', '-4'), ('-2', '-3', '4'), ('-2', '-3', '-4')], [('2', '4', '3'), ('2', '4', '-3'), ('2', '-4', '3'), ('2', '-4', '-3'), ('-2', '4', '3'), ('-2', '4', '-3'), ('-2', '-4', '3'), ('-2', '-4', '-3')], [('3', '2', '4'), ('3', '2', '-4'), ('3', '-2', '4'), ('3', '-2', '-4'), ('-3', '2', '4'), ('-3', '2', '-4'), ('-3', '-2', '4'), ('-3', '-2', '-4')], [('3', '4', '2'), ('3', '4', '-2'), ('3', '-4', '2'), ('3', '-4', '-2'), ('-3', '4', '2'), ('-3', '4', '-2'), ('-3', '-4', '2'), ('-3', '-4', '-2')], [('4', '2', '3'), ('4', '2', '-3'), ('4', '-2', '3'), ('4', '-2', '-3'), ('-4', '2', '3'), ('-4', '2', '-3'), ('-4', '-2', '3'), ('-4', '-2', '-3')], [('4', '3', '2'), ('4', '3', '-2'), ('4', '-3', '2'), ('4', '-3', '-2'), ('-4', '3', '2'), ('-4', '3', '-2'), ('-4', '-3', '2'), ('-4', '-3', '-2')]]
Demo :
first we must create the permutations of your number list to have all the states :
>>> p=list(permutations(['2','3','4']))
[('2', '3', '4'), ('2', '4', '3'), ('3', '2', '4'), ('3', '4', '2'), ('4', '2', '3'), ('4', '3', '2')]
then create a permutations like above for negative numbers :
>>>[('-'+i,'-'+j,'-'+k) for i,j,k in permutations(['2','3','4'])]
[('-2', '-3', '-4'), ('-2', '-4', '-3'), ('-3', '-2', '-4'), ('-3', '-4', '-2'), ('-4', '-2', '-3'), ('-4', '-3', '-2')]
and then zip
2 preceding permutations for use them in product statement :
>>> new=zip(l,p)