2011-03-09 138 views
2

因此,這是我嘗試生成具有Itertools和Python二進制表

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

但這生成[( '0', '0'),( '0', '1'),(」 1','1')]

我仍然需要一個('1','0')元組,有沒有辦法讓itertools也做組合和順序?

回答

3

使用

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

代替。

+2

只是'列表生成數字(itertools.product( 「01」,重複= 2))' – Kabie 2011-03-09 17:39:29

4

要拍攝值的笛卡爾積與本身,你用

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

這會給你所有可能的組合。

0

此程序從1到100,然後將其轉換爲二進制

a=0 
while a<100: 
a=a+1 
print a,"=",bin(a)