使用Itertools和Python生成二进制表

w-l*_*-ll 2 python binary python-itertools

所以这就是我的尝试

list(itertools.combinations_with_replacement('01', 2))
Run Code Online (Sandbox Code Playgroud)

但这会产生[('0','0'),('0','1'),('1','1')]

我仍然需要一个('1','0')元组,有没有办法让itertools也做组合和命令?

Sve*_*ach 5

list(itertools.product(*["01"] * 2))
Run Code Online (Sandbox Code Playgroud)

反而。

  • 只是`list(itertools.product("01",repeat=2))` (2认同)

sha*_*ang 5

要使用自身的笛卡尔积,请使用

itertools.product("01", repeat=2)
Run Code Online (Sandbox Code Playgroud)

这将为您提供所有可能的组合.