python散装笔记——23: groupby()

python散装笔记——23: groupby()

编码文章call10242025-02-01 3:50:259A+A-


Parameter

Details

iterable

任何 python 可迭代

key

函数(标准),据此对可迭代对象进行分组

在 Python 中,itertools.groupby() 方法允许开发人员根据指定属性将可迭代类的值分组到另一个可迭代值集中。

Section 23.1: 示例 1

在这个示例中,我们可以看到使用不同类型的可迭代程序时会发生什么。

things = [("animal", "bear"), ("animal", "duck"), ("plant", "cactus"), ("vehicle", "harley"), ("vehicle", "speed boat"), ("vehicle", "school bus")]
dic = {}
f = lambda x: x[0]
for key, group in groupby(sorted(things, key=f), f):
    dic[key] = list(group)
dic

结果如下

{'animal': [('animal', 'bear'), ('animal', 'duck')],
 'plant': [('plant', 'cactus')],
 'vehicle': [('vehicle', 'harley'),
             ('vehicle', 'speed boat'),
             ('vehicle', 'school bus')]}

下面的示例与上面的示例基本相同。唯一不同的是,我把所有元组都改成了列表。

things = [["animal", "bear"], ["animal", "duck"], ["vehicle", "harley"], 
          ["plant","cactus"], ["vehicle", "speed boat"], ["vehicle", "school bus"]]
dic = {}
f = lambda x: x[0]
for key, group in groupby(sorted(things, key=f), f):
  dic[key] = list(group)
dic 

成果

{'animal': [['animal', 'bear'], ['animal', 'duck']],
 'plant': [['plant', 'cactus']],
 'vehicle': [['vehicle', 'harley'],
             ['vehicle', 'speed boat'],
             ['vehicle', 'school bus']]}

Section 23.2: 示例 2

本例说明了如果我们不指定任何 key,将如何选择默认 key

c = groupby(['goat', 'dog', 'cow', 1, 1, 2, 3, 11, 10, ('persons', 'man', 'woman')])
dic = {}
for k, v in c:
    dic[k] = list(v)
dic

结果

{1: [1, 1],
 2: [2],
 3: [3],
 ('persons', 'man', 'woman'): [('persons', 'man', 'woman')],
 'cow': ['cow'],
 'dog': ['dog'],
 10: [10],
 11: [11],
 'goat': ['goat']}

请注意,在这个列表中,元组作为一个整体算作一个键

Section 23.3: 示例 3

请注意,在这个示例中,mulatocamel 并没有出现在我们的结果中。只有指定键的最后一个元素才会出现。c 的最后一个结果实际上抹去了之前的两个结果。但请看我的新版本,在这个版本中,数据首先按相同的键排序。

list_things = ['goat', 'dog', 'donkey', 'mulato', 'cow', 'cat', 
               ('persons', 'man', 'woman'), 'wombat', 'mongoose', 'malloo', 'camel']
c = groupby(list_things, key=lambda x: x[0])
dic = {}
for k, v in c:
    dic[k] = list(v)
dic

结果

{'c': ['camel'],
 'd': ['dog', 'donkey'],
 'g': ['goat'],
 'm': ['mongoose', 'malloo'],
 'persons': [('persons', 'man', 'woman')],
 'w': ['wombat']}

排序版本

list_things = ['goat', 'dog', 'donkey', 'mulato', 'cow', 'cat', 
               ('persons', 'man', 'woman'),'wombat', 'mongoose', 'malloo', 'camel']
sorted_list = sorted(list_things, key = lambda x: x[0])
print(sorted_list)
print()
c = groupby(sorted_list, key=lambda x: x[0])
dic = {}
for k, v in c:
    dic[k] = list(v)
dic

结果

['cow', 'cat', 'camel', 'dog', 'donkey', 'goat', 'mulato', 'mongoose', 'malloo', ('persons', 'man', 'woman'), 'wombat']
{'c': ['cow', 'cat', 'camel'],
 'd': ['dog', 'donkey'],
 'g': ['goat'],
 'm': ['mulato', 'mongoose', 'malloo'],
 'persons': [('persons', 'man', 'woman')],
 'w': ['wombat']}
点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4