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

listに含まれる要素で組合せを作る

Posted at

リストの要素の組合せリスト作成時の備忘録_φ(・_・

目的:listに含まれる要素で組合せを作る

下記のリストを例にします。

data = [0,1,2,3]

方法1:重複あり

sample1.py
for i in data:
    for j in data:
        print(i, j)

# 出力結果
# 0 0
# 0 1
# 0 2
# 0 3
# 1 0
# 1 1
# 1 2
# 1 3
# 2 0
# 2 1
# 2 2
# 2 3
# 3 0
# 3 1
# 3 2
# 3 3

方法2:重複なし

sample2.py
for i in data:
    for j in [x for x in data if x != i]:
        print(i, j)

# 出力結果
# 0 1
# 0 2
# 0 3
# 1 0
# 1 2
# 1 3
# 2 0
# 2 1
# 2 3
# 3 0
# 3 1
# 3 2

完全に自分の備忘録のため。

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