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

[python] more_itertools.bucket

Last updated at Posted at 2025-08-06

概要

イテレータに対してgroupbyのようなことができる

使用例

以下のようにkeyを指定することでイテレータに対する上位グループを作成することが可能

iterable = ['a1', 'b1', 'c1', 'a2', 'b2', 'c2', 'b3']
s = bucket(iterable, key=lambda x: x[0])  # Bucket by 1st character
sorted(list(s))  # Get the keys
# ['a', 'b', 'c']
a_iterable = s['a']
next(a_iterable)
# 'a1'
next(a_iterable)
# 'a2'
list(s['b'])
# ['b1', 'b2', 'b3']

公式リファレンス
https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.bucket

参考記事
https://qiita.com/SaitoTsutomu/items/ddb5076ef62745f03b56#more_itertools%E3%81%AE%E7%B4%B9%E4%BB%8B:~:text=%E7%B5%90%E6%9E%9C-,bucket(),-iterable%2C%20key%2C%20validator

1
0
0

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