1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

python: 内包表記で組合せ(コンビネーション)を列挙する

Last updated at Posted at 2021-10-31

異なるn個のものからr個のものを選び出す組合せ(コンビネーション)を列挙する。

5個から2個を選ぶ
[[a, b] for a in range(5) for b in range(a)]
 [[1, 0],
 [2, 0],
 [2, 1],
 [3, 0],
 [3, 1],
 [3, 2],
 [4, 0],
 [4, 1],
 [4, 2],
 [4, 3]]
5個から2個を選ぶ(ビットパターン)
[1<<a | 1<<b for a in range(5) for b in range(a)]
[3, 5, 6, 9, 10, 12, 17, 18, 20, 24]
6個から3個を選ぶ
[[a, b, c] for a in range(6) for b in range(a) for c in range(b)]
[[2, 1, 0],
 [3, 1, 0],
 [3, 2, 0],
 [3, 2, 1],
 [4, 1, 0],
 [4, 2, 0],
 [4, 2, 1],
 [4, 3, 0],
 [4, 3, 1],
 [4, 3, 2],
 [5, 1, 0],
 [5, 2, 0],
 [5, 2, 1],
 [5, 3, 0],
 [5, 3, 1],
 [5, 3, 2],
 [5, 4, 0],
 [5, 4, 1],
 [5, 4, 2],
 [5, 4, 3]]
1
0
0

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?