Suppose, I have a list of
1,1
and it can take either + or - sign. So the possible combination would be 2 to the power 2.
1 1
1 -1
-1 1
-1 -1
Similarly, I have a list of
1,1,1
and it can take either + or - sign. So the possible combination would be 2 to the power 3.
-1 1 -1
-1 1 1
1 1 1
1 -1 1
-1 -1 -1
1 1 -1
1 -1 -1
-1 -1 1
In python, how can I do that using itertools or any other methods. Any help please.
Let’s say you have a list that looks like this: ['a', 'b', 'c']. When you create a list of all possible combinations, you’ll end up with a list that looks like this: [ (), ('a',), ('b',), ('c',), ('a', 'b'), ('a', 'c'), ('b', 'c'), ('a', 'b', 'c')].
This diagram shows the nomenclature for the different phase transitions. The term phase transition (or phase change) is most commonly used to describe transitions between solid, liquid, and gaseous states of matter, as well as plasma in rare cases.
To create the list of all possible combinations: Click the Expand button in the column header. The list of possible combinations now appears in the Power Query window. If you have more than two queries, go ahead and expand those columns too. If you’re working with the example data, the Preview window now looks like this:
Goldenfeld, N., Lectures on Phase Transitions and the Renormalization Group, Perseus Publishing (1992). M.R.Khoshbin-e-Khoshnazar, Ice Phase Transition as a sample of finite system phase transition, (Physics Education (India)Volume 32. No. 2, Apr - Jun 2016) [1] Kleinert, H., Gauge Fields in Condensed Matter, Vol.
>>> import itertools
>>> lst = [1,1,1]
>>> for xs in itertools.product([1,-1], repeat=len(lst)):
... print([a*b for a,b in zip(lst, xs)])
...
[1, 1, 1]
[1, 1, -1]
[1, -1, 1]
[1, -1, -1]
[-1, 1, 1]
[-1, 1, -1]
[-1, -1, 1]
[-1, -1, -1]
You can do:
from itertools import combinations
size = 3
ans = list(set(combinations([-1,1]*size,size)))
#[(1, 1, -1),
# (-1, 1, 1),
# (-1, -1, 1),
# (1, -1, -1),
# (1, -1, 1),
# (-1, 1, -1),
# (1, 1, 1),
# (-1, -1, -1)]
This approach also gives the same result with permutations
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With