from itertools import groupby
作者:互联网
需求:期望由 a 得到 b
实现方法:
from itertools import groupby a = [ {'name': 'a', 'value': 1}, {'name': 'b', 'value': 1}, {'name': 'a', 'value': 2}, ] b = [['a', 'b'], ['a']] lst = [] for key, group in groupby(a, key=lambda x: x['value']): lst.append([item['name'] for item in group]) print(lst)
标签:group,name,lst,value,item,itertools,import,groupby 来源: https://www.cnblogs.com/fivenian/p/15785826.html