from collections import defaultdict
def group_nested_dict_by_common_value(nested_dict, nest_key):
result_dict = defaultdict(list)
for key, val in nested_dict.items():
result_dict[val[nest_key]].append((key, val))
return dict(result_dict)
# 示例用法
nested_dict = {
'a': {'b': 1, 'c': 2},
'd': {'b': 3, 'c': 2},
'e': {'b': 1, 'c': 4}
}
nest_key = 'c'
result = group_nested_dict_by_common_value(nested_dict, nest_key)
print(result)
# {'2': [('a', {'b': 1, 'c': 2}), ('d', {'b': 3, 'c': 2})], '4': [('e', {'b': 1, 'c': 4})]}