順列、階乗 (a, b, c, d, e)の全ての並べ方は $5! = 5\times 4 \times 3 \times 2 \times 1 = 120 $通りです。 順列を求めるときにはpermutationsを使用します。 >>> list(itertools.permutations(seq)) [('a', 'b', 'c', 'd', 'e'), ('a', 'b', 'c', 'e', 'd'), ('a', 'b', 'd', 'c', 'e'), ('a', 'b', 'd', 'e', 'c'), 中略 ('e', 'd', 'c', 'a', 'b'), ('e', 'd', 'c', 'b', 'a')] >>> len(list(itertools.permutations(seq))) 120 120個の要素を持つリストができたことが分かります。 次に、(a,
●全要素のイテレータが欲しい場合(import itertools) 直積(デカルト積):itertools.product 例: list(itertools.product([‘a’, ‘b’, ‘c’, ‘d’, ‘e’], repeat=2)) #= itertools.product([‘a’, ‘b’, ‘c’, ‘d’, ‘e’], [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]) [(‘a’, ‘a’), (‘a’, ‘b’), (‘a’, ‘c’), (‘a’, ‘d’), (‘a’, ‘e’), (‘b’, ‘a’), (‘b’, ‘b’), (‘b’, ‘c’), (‘b’, ‘d’), (‘b’, ‘e’), (‘c’, ‘a’), (‘c’, ‘b’), (‘c’, ‘c’), (‘c’, ‘d’), (‘c’, ‘e’), (‘d’, ‘a’), (‘d’,
>>> import Data.List >>> permutations [1,2,3] [[1,2,3],[2,1,3],[3,2,1],[2,3,1],[3,1,2],[1,3,2]] comb :: Int -> [a] -> [[a]] comb 0 xs = [[]] comb _ [] = [] comb n (x:xs) = [x:y | y <- comb (n-1) xs] ++ comb n xs >>> import Control.Monad >>> replicateM 5 ["a", "b", "c"] [["a","a","a","a","a"],["a","a","a","a","b"],["a","a","a","a","c"],["a","a","a","b","a"],["a","a","a","b","b"],["a","a","a","b","c
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く