Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

all possible phase combination

Tags:

python

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.

like image 288
user2095624 Avatar asked Jun 11 '13 06:06

user2095624


People also ask

What does a list of all possible combinations look like?

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')].

What are the different phase transitions?

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.

How do I create a list of all possible combinations?

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:

What are the best books on finite system phase transition?

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.


2 Answers

>>> 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]
like image 100
falsetru Avatar answered Oct 09 '22 10:10

falsetru


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.

like image 29
Saullo G. P. Castro Avatar answered Oct 09 '22 12:10

Saullo G. P. Castro