Consider the following simple case:
list={1,2,2};
Subsets[{Splice[list],Splice[-list]},{1,Length@list}]
This produces the following combinations:
{{1}, {2}, {2}, {-1}, {-2}, {-2}, {1, 2}, {1, 2}, {1, -1}, {1, -2}, {1, -2}, {2, 2}, {2, -1}, {2, -2}, {2, -2}, {2, -1}, {2, -2}, {2, -2}, {-1, -2}, {-1, -2}, {-2, -2}, {1, 2, 2}, {1, 2, -1}, {1, 2, -2}, {1, 2, -2}, {1, 2, -1}, {1, 2, -2}, {1, 2, -2}, {1, -1, -2}, {1, -1, -2}, {1, -2, -2}, {2, 2, -1}, {2, 2, -2}, {2, 2, -2}, {2, -1, -2}, {2, -1, -2}, {2, -2, -2}, {2, -1, -2}, {2, -1, -2}, {2, -2, -2}, {-1, -2, -2}}
The problem I am trying to solve requires me to have all possible combinations of list
but in any given combination, if an entry appears then it either appears as positive or negative and not both.
So for example {2, -2, -2}
is invalid since a given 2
should appear as positive or negative but here there are three 2
s appearing even though the original list
had two 2
s.
Also, I am looking for an efficient way to generate the required combination.
tal = Lookup[Counts[list], Union[list], 0]; Select[Subsets[{Splice[list], Splice[-list]}, {1, Length[list]}], And @@ Thread[Lookup[Counts[Abs[#]], Union[list], 0] <= tal] &]
do what you want? $\endgroup$