如果存在多个值,请从defaultdict中提取值



我得到了名为"anagrams"的默认dict,它看起来像这样:

defaultdict(set,
{'a': {'a'},
'act': {'act', 'cat', 'tac'},
'hitw': {'with'},
'acn': {'can'},
'eikl': {'like'},
'ehty': {'they'},
'eilnst': {'listen', 'silent'},
'or': {'or'},
'be': {'be'}})

现在,如果有多个值分配给一个键,我只想列出这些值。我一直在网上搜索一个可以做到这一点的(列表(理解,但我运气不好。

结果应该是:

[{'act', 'cat', 'tac'}, {'listen', 'silent'}]

有人能给个提示吗?

>>> d = {'a': {'a'},
...              'act': {'act', 'cat', 'tac'},
...              'hitw': {'with'},
...              'acn': {'can'},
...              'eikl': {'like'},
...              'ehty': {'they'},
...              'eilnst': {'listen', 'silent'},
...              'or': {'or'},
...              'be': {'be'}}
>>>
>>> [v for v in d.values() if len(v) > 1]
[{'act', 'cat', 'tac'}, {'listen', 'silent'}]

最新更新