差分
このページの2つのバージョン間の差分を表示します。
| python:itertools [2017/12/08 16:24] – 作成 mumeiyamibito | python:itertools [2017/12/08 16:43] (現在) – mumeiyamibito | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| ====== Python モジュール: | ====== Python モジュール: | ||
| ===== 概要 ===== | ===== 概要 ===== | ||
| - | * 順列や組み合わせ、直積を簡単に扱えるモジュール | + | * 階乗、順列や組み合わせ、直積を簡単に扱えるモジュール | 
| ===== 使い方 ===== | ===== 使い方 ===== | ||
| 行 11: | 行 11: | ||
| VAR = list(itertools.permutations(SEQ)) | VAR = list(itertools.permutations(SEQ)) | ||
| </ | </ | ||
| - |     * '' | + |     * '' | 
| - |     * '' | + |     * '' | 
|     * パターン数を求める場合は、'' |     * パターン数を求める場合は、'' | ||
| * 例: 5! のパターン数\\ <code python> | * 例: 5! のパターン数\\ <code python> | ||
| 行 24: | 行 24: | ||
| ==== 順列 ==== | ==== 順列 ==== | ||
| * 順列のイテレータ (list でリストに変換できる) を作成する\\ <code python> | * 順列のイテレータ (list でリストに変換できる) を作成する\\ <code python> | ||
| - | VAR = itertools.permutations(SEQ, | + | VAR = list(itertools.permutations(SEQ, | 
| + | </ | ||
| + |     * '' | ||
| + |     * '' | ||
| + |     * '' | ||
| + |     * 例: ${}_5 \mathrm{P}_3$\\ <code python> | ||
| + | variable = len(list(itertools.permutations(range(5), | ||
| </ | </ | ||
| - |     * '' | ||
| - |     * '' | ||
| ==== 組み合わせ ==== | ==== 組み合わせ ==== | ||
| + | * 組み合わせ (順列の順序なし) のイテレータ (list でリストに変換できる) を作成する\\ <code python> | ||
| + | VAR = list(itertools.combinations(SEQ, | ||
| + | </ | ||
| + |     * '' | ||
| + |     * '' | ||
| + |     * '' | ||
| + | * 例: ${}_5 \mathrm{C}_3$\\ <code python> | ||
| + | variable = len(list(itertools.combinations(range(5), | ||
| + | </ | ||
| + | ==== 直積 ==== | ||
| + |   * 2 つの集合体の組み合わせである直積 ($A \times B$) のイテレーターを作成する\\ <code python> | ||
| + | VAR = list(itertools.product(SEQ_A, | ||
| + | </ | ||
| + |     * '' | ||
| + |     * '' | ||
| + |     * '' | ||
| + | * 返り値は、組み合わせパターンの 2 変数 | ||
| + |     * 例:\\ <code python> | ||
| + | list_a = [1,2,3] | ||
| + | list_b = [4,5] | ||
| + | variable = len(list(itertools.product(list_a, | ||
| + | # [(1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)] | ||
| + | for i,j in itertools.product(list_a, | ||
| + | print(i, j) | ||
| + | # 1 4 | ||
| + | # 1 5 | ||
| + | # 2 4 | ||
| + | # 2 5 | ||
| + | # 3 4 | ||
| + | # 3 5 | ||
| + | </ | ||
| ===== 参考サイト ===== | ===== 参考サイト ===== | ||