处理字典词典

IUn*_*own 2 python dictionary

我有一本字典 - 其中的值是字典本身.
如何以最有效的方式从子词典中提取唯一的值集?

{ 'A':{'A1':'A1V','B2':'A2V'..},
  'B':{'B1':'B1V','B2':'B2V'...},
  ...}
Run Code Online (Sandbox Code Playgroud)

预期产量:

['A1V','A2V','B1V','B2V'...]
Run Code Online (Sandbox Code Playgroud)

MSe*_*ert 5

在一行中:

>>> [val for dct in x.values() for val in dct.values()]
['A1V', 'A2V', 'B2V', 'B1V']
Run Code Online (Sandbox Code Playgroud)

假设你命名了dict dict x.

你提到了unique,在这种情况下用set-comprehension替换list-comprehension:

>>> {val for dct in x.values() for val in dct.values()}  # curly braces!
{'A1V', 'A2V', 'B1V', 'B2V'}
Run Code Online (Sandbox Code Playgroud)