Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Binary Table with Itertools and Python

So this is what im trying

list(itertools.combinations_with_replacement('01', 2))

but this is generating [('0', '0'), ('0', '1'), ('1', '1')]

I still need a ('1','0') tuple, is there a way to make itertools also do combinations and order?

like image 897
w-ll Avatar asked Dec 28 '22 00:12

w-ll


2 Answers

Use

list(itertools.product(*["01"] * 2))

instead.

like image 98
Sven Marnach Avatar answered Jan 06 '23 14:01

Sven Marnach


To take the cartesian product of a value with itself, you use

itertools.product("01", repeat=2)

This will give you all possible combinations.

like image 32
shang Avatar answered Jan 06 '23 16:01

shang