Pythonのitertools.product()関数

itertoolsは、イテレータを提供するライブラリです。itertools.product()関数は、複数の反復可能なオブジェクトのデカルト積を返します。

例1:2つのリストのデカルト積

同等のサイクルは次のとおりです。
(x,y) for x in A for y in B

from itertools import *
for i in product([7,8,9],[1,2,3]):
	print (i)

出力:

(7, 1)
(7, 2)
(7, 3)
(8, 1)
(8, 2)
(8, 3)
(9, 1)
(9, 2)
(9, 3)

例2:

合計3つのポジション、各ポジションは0または1と表示される可能性があります

for i in product([0,1],repeat=3):
	print (i)

出力:

(0, 0, 0)
(0, 0, 1)
(0, 1, 0)
(0, 1, 1)
(1, 0, 0)
(1, 0, 1)
(1, 1, 0)
(1, 1, 1)

おすすめ

転載: blog.csdn.net/qq_45268474/article/details/108504294