itertools.groupby()用作什么?

itertools.groupby()用作什么?,第1张

itertools.groupby()用作什么?

首先,您可以在此处阅读文档。

我将最重要的观点放在首位。我希望这些例子之后的原因将变得清楚。

始终对具有相同密钥的项进行排序,以便避免意外的结果

itertools.groupby(iterable, key=None or some func)

取得可迭代项的列表,并根据指定的密钥对它们进行分组。键指定对每个可迭代对象要执行的 *** 作,然后将其结果用作每个对项进行分组的标题;最终具有相同“键”值的项目将归入同一组。

返回值的形式类似于字典,是可迭代的

{key : value}

例子1

# note here that the tuple counts as one item in this list. I did not# specify any key, so each item in the list is a key on its own.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], 'goat': ['goat'], 3: [3], 'cow': ['cow'], ('persons', 'man', 'woman'): [('persons', 'man', 'woman')], 10: [10], 11: [11], 2: [2], 'dog': ['dog']}

例子2

# notice here that mulato and camel don't show up. only the last element with a certain key shows up, like replacing earlier result# the last result for c actually wipes out two previous results.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']}

现在为排序版本

 # but observe the sorted version where I have the data sorted first on same key I used for groupinglist_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']}

例子3

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']]}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5632116.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存