-2

Because of unclear description, I reformatted my question as the way @user2346536 told me, thanks @user2346536.

Questions: I would like to get a list which contains all kinds of possibilities of elements like below:

Given a set of numbers, I want to generate all the 3-Combinations of them. Also, In such combination any number can be replaced by its opposite. Hence for (2,3,4) --> [3,2,4], [2,3,4] and [-2,3,4] are all valid 'combinations' whereas [-2,2,4] or [3,3,4] are not.

Has been done perfectly by @Kasra, thanks for kind help. ....Need to learn how to give a question...

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Leu_Grady
  • 498
  • 3
  • 15
  • 4
    *a list like this* isn't a clear description of what rules govern the creation of that list. How are those numbers produced? All combinatinos of `[2, 3, 4]` and their negatives in any order? – Martijn Pieters Jan 06 '15 at 08:22
  • Yes, sorry to bother you without good enough description! – Leu_Grady Jan 06 '15 at 08:24
  • 1
    as @MartijnPieters said: formally define this list and add the code you've written so far. – reto Jan 06 '15 at 08:24
  • what kind of loops have you tried? And do you care about the order the sublists appear in? – Joel Jan 06 '15 at 08:24
  • 1
    thanks to Stack overflow... http://stackoverflow.com/questions/4941753/is-there-a-math-ncr-function-in-python I guess you want something like this ? for x in itertools.combinations( [2,-2,3,-3,4,-4],3): print (x) Note: you need to "import itertools" first. – user2346536 Jan 06 '15 at 08:29
  • I wanted to post it as an answer, but the question has been closed. – user2346536 Jan 06 '15 at 08:30
  • Sorry, maybe i do not explain very clearly before. One number and its negative form can not appear at same time. @user2346536 – Leu_Grady Jan 06 '15 at 08:41
  • I guess @Kasra answer is what you need then. Trying to formulate your question: "Given a set of numbers, I want to generate all the 3-Combinations of them. Also, In such combination any number can be replace by its opposite." Hence for ( 2,3,4 ) --> [3,2,4], [2,3,4] and [-2,3,4] are all valid 'combinations' whereas [-2,2,4] or [3,3,4] are not. – user2346536 Jan 06 '15 at 08:48
  • Here's a one liner: `[list(x) for a in [-2,2] for b in [-3,3] for c in [-4,4] for x in itertools.permutations([a,b,c])]` – Joel Jan 06 '15 at 08:53
  • Here's a general solution. `from itertools import permutations;seq = range(2, 5);print sum((list(permutations(v)) for v in ((i if i in u else -i for i in seq) for u in reduce(lambda z, x: z + [y + [x] for y in z], seq, [[]]))), [])` – PM 2Ring Jan 06 '15 at 09:01

1 Answers1

2

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)
Mazdak
  • 105,000
  • 18
  • 159
  • 188