需求:期望由 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)