Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating combinations/permutations in python of a boolean list without using ITERTOOLS [closed]

l = [True, True , False]

Without using itertools module.

How do yo create permutations of l in a new list

newlist = [[True,True,False],[True,True,True], [False,False,True],[False,False,False]....]

essentially this is what im trying to do:

allorderings = itertools.product ([False, True], repeat = n)
like image 322
user3349106 Avatar asked Apr 08 '14 06:04

user3349106


3 Answers

Use itertools.permutations

import itertools
l = [True, True , False]
newlist = list(itertools.permutations(l))

EDIT: from your question, one of the permutations you need is (True, True, True) which is not a permutation at all of the list l. This answer gives you permutations of a list in the technical sense and you may have to do extra work to achieve what you show in the question(unless of course that was a typo).

like image 103
yati sagade Avatar answered Nov 14 '22 22:11

yati sagade


The simplest way I could think of, would be to iterate over the same list of items thrice and collect only the unique items, like this

l = set([True, True, False])
print {(i, j, k) for i in l for j in l for k in l}
like image 38
thefourtheye Avatar answered Nov 14 '22 22:11

thefourtheye


Use itertools.permutations's equivalent in pure python from the official docs?

like image 24
bravmi Avatar answered Nov 14 '22 23:11

bravmi