57
36

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で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次元リスト中の重複した行を削除する

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?