5
4

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 5 years have passed since last update.

Pythonで組み合わせの問題

Last updated at Posted at 2015-11-01

Pythonで組み合わせの問題を解いてみたが、いい方法が思いつかない。。。

letter = ["A","B","C"]

for i in range(3):
	for j in range(3):
		for k in range(3):
			print letter[i], letter[j], letter[k]

これはさすがに幼稚すぎるし、リストの長さが増えると。。。

ということで、調べてみると便利なライブラリがあった。

import itertools

letter = ["A","B","C"]
for i in itertools.product(letter, repeat=len(letter)):
	print i

これならたったの2、3行で済んだ。

5
4
2

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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?