慕无忌1623718
您可以构建一个包含每对键和值的集合,并从中构建字典条目:dict1 = { 'a':['b','c'], 'd':['e','f'] }sets = [set([key]) | set(values) for key, values in dict1.items() ]# [{'a', 'b', 'c'}, {'f', 'd', 'e'}]out = {}for s in sets: for key in s: out[key] = list(s-set([key]))print(out)输出:{'a': ['b', 'c'], 'b': ['a', 'c'], 'c': ['a', 'b'], 'f': ['d', 'e'], 'd': ['f', 'e'], 'e': ['f', 'd']}