python 多次分组 group by 嵌套字典(nested dict)
需求:在日常开发中,经常会遇到对某一可迭代序列多维度分组,得到按照维度先后的嵌套字典,例如,得到整个MySQL的information_schema.columns表中的所有数据,像动态生成和表对应的类,简化持久化的操作。之中就需要对每个columns里的数据进行db和table两个维度分组,把在同db且同table的数据组合在一起。
举个更简单的例子:
itertools.permutations(range(4), 3) # 0,1,2三个数字排列组合
def group2nesteddict(iterable, keys, idx=0): ''' group iterable to nested dict by keys , nested deep depend keys length @param keys: functions for group @return: defaultdict >>> import itertools >>> group2nesteddict(itertools.permutations(range(4), 3), [lambda x:x[0], lambda x:x[2]]) defaultdict(<type 'list'> , {0: defaultdict(<type 'list'>,{1: [(0, 2, 1), (0, 3, 1)] , 2: [(0, 1, 2), (0, 3, 2)] , 3: [(0, 1, 3), (0, 2, 3)]}), 1: defaultdict(<type 'list'>,{0: [(1, 2, 0), (1, 3, 0)] , 2: [(1, 0, 2), (1, 3, 2)] , 3: [(1, 0, 3), (1, 2, 3)]}), 2: defaultdict(<type 'list'>,{0: [(2, 1, 0), (2, 3, 0)] , 1: [(2, 0, 1), (2, 3, 1)] , 3: [(2, 0, 3), (2, 1, 3)]}), 3: defaultdict(<type 'list'>,{0: [(3, 1, 0), (3, 2, 0)] , 1: [(3, 0, 1), (3, 2, 1)] , 2: [(3, 0, 2), (3, 1, 2)]})}) ''' x = reduce(lambda d, x:d.append_2_value_collection(keys[idx](x), x), iterable, GroupDict()) if idx != len(keys) - 1: for k, v in x.items(): x[k] = group2nesteddict(v, keys, idx + 1) return x