LoginSignup
55
36

More than 5 years have passed since last update.

Pythonで2次元配列の重複行を一発で削除する

Last updated at Posted at 2017-09-28

したいこと

重複する要素を持つ2次元配列から、ダブりを削除したい。
つまり、これを、、、、

[[0, 0],
[1, 1],
[1, 0],
[1, 1],
[0, 1],
[0, 0]]

こうしたい!

[[0, 1],
[1, 0],
[0, 0],
[1, 1]]

やりかた

  1. 各要素をtupleに変換
  2. setで重複を削除
  3. listに戻す

※ポイントはtupleに変換するとこ。list型だとsetが使えない。

>>> arr = [[0,0], [1,1], [1,0], [1,1], [0,1], [0,0]]
>>> arr = list(map(list, set(map(tuple, arr))))
>>> arr
[[0, 1], [1, 0], [0, 0], [1, 1]]

なにか、ほかにいい方法あれば教えてください〜

お世話になったページ

集合型で2次元リスト中の重複した行を削除する

55
36
5

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
55
36