LoginSignup
2
0

More than 5 years have passed since last update.

任意の配列n個の中身をループを回して1つずつ全通りの組合せを表示する。

Last updated at Posted at 2018-01-02

やったこと

任意の配列n個の中身をループを回して1つずつ全通りの組合せを表示する。

narray.py
# coding:utf-8
import itertools

dim_num = 3
b = [chr(i) for i in range(65,65+dim_num)]

for b in itertools.product(range(3),repeat=dim_num):
    print(b)

実行結果

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

こうとかもいける。

narray.py
# coding:utf-8
import itertools
import numpy as np

dim_num = 3
b = [chr(i) for i in range(65,65+dim_num)]
array = np.array(([[[3,3],[2,1]],[[2,1],[3,2]]]))

for b in itertools.product(range(2),repeat=dim_num):
    print(array[b])

実行結果

[[[3 3]
  [2 1]]

 [[2 1]
  [3 2]]]
3
3
2
1
2
1
3
2

参考文献

アルファベットのリストを作る in python
itertools — 効率的なループ実行のためのイテレータ生成関数

2
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0