合并列表中的字典并获取相等键的值集

Merge dicts from list with getting set of values for equal keys

我有机会用字典增强重写下面的代码吗? (如果我命名正确,意思是 {k: v for k, v in ...}

list_of_dicts = [{'a': 1}, {'b': 2}, {'b': 20, 'c': 3}, {'a': 10, 'b': 2}]
for k, v in [p for d in list_of_dicts  for p in d.items()]:
    d[k] = d.setdefault(k, set()) | {v}

确定为什么不 :)。但它嵌套在

import itertools
list_of_dicts = [{'a': 1}, {'b': 2}, {'b': 20, 'c': 3}, {'a': 10, 'b': 2}]
o = {k: {d[k] for d in list_of_dicts if k in d} for k in itertools.chain.from_iterable(list_of_dicts)}
print(o)