12
23

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 2020-06-09

似たような記事はいくつかありますがcollections.Counterを使う方法が簡単かつ便利だと思うので置いておきます。

from collections import Counter

hoge = ['a', 'b', 'c', 'd', 'e']
fuga = ['c', 'd', 'e', 'f', 'g']

Counter(hoge + fuga)
# => Counter({'a': 1, 'b': 1, 'c': 2, 'd': 2, 'e': 2, 'f': 1, 'g': 1})

# 2つ以上の要素
[k for k, v in Counter(hoge + fuga).items() if v > 1]
# => ['c', 'd', 'e']
12
23
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
12
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?