If I understand your goal correctly, you want to create an if
statement that creates all arrangements of your string
variable, then test if your "string" is in any of those arrangements.
All arrangements of a list would be the "permutations", so you can use permutation of itertools module:
import itertools
string = ("red blue black")
all_arrangements = list(itertools.permutations(string.split()))
if string in [' '.join(i) for i in all_arrangements]:
print "cool"
Since the arrangements are generated based on your string, you can expect your if statement will be satisfied, and "cool" will always be printed, unless you further change the logic of the if
statement.
To better explain what's going on in the code above, you could replace the above if
statement block with the following code, and confirm which arrangement of your string matches:
print "checking for ", string
for arrangement in all_arrangements:
if string == ' '.join(arrangement):
print arrangement , "cool"
else:
print arrangement
output:
checking for red blue black
('red', 'blue', 'black') cool
('red', 'black', 'blue')
('blue', 'red', 'black')
('blue', 'black', 'red')
('black', 'red', 'blue')
('black', 'blue', 'red')
Hope this helps.