# -*- coding: utf-8 -*-
# @Time    : 2018/8/31 14:32
# @Author  : cxa
# @File    : glomtest.py
# @Software: PyCharm
from operator import itemgetter
from itertools import groupby

a= (('a', 1),('a', 2),('b', 1),('b', 5),('c', 6),('c', 2),('d', 1),('d', 21))
b=list(a)
b.sort(key=itemgetter(0,1))
data_2=groupby(b,itemgetter(0))

for k,v in data_2:
    print(k,list(v))

输出

a [('a', 1), ('a', 2)]
b [('b', 1), ('b', 5)]
c [('c', 2), ('c', 6)]
d [('d', 1), ('d', 21)]

相关文章:

  • 2022-12-23
  • 2021-08-21
  • 2021-08-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-08
  • 2022-12-23
猜你喜欢
  • 2021-11-27
  • 2022-12-23
  • 2021-06-12
  • 2021-12-31
  • 2021-12-15
  • 2022-12-23
  • 2021-09-25
相关资源
相似解决方案