LoginSignup
6
6

More than 5 years have passed since last update.

Pythonでリストの連続する要素でグループ化するgroupby

Posted at
from itertools import groupby

a = [1, 2, 3, 2, 2, 2, 3, 3, 4, 4, 4, 3, 1, 1]
a = sorted(a)

# 連続する要素をグループ化する
for key, group in groupby(a):
    print list(group)

実行結果

[1, 1, 1]
[2, 2, 2, 2]
[3, 3, 3, 3]
[4, 4, 4]
6
6
1

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
6
6