在可见字典的值中识别重复的键值对



我有这本字典:

Ip = {'a':['one','two','three'],'b':['one','five','six'],'c':['seven','eight','nine'],'d':['five']}

我希望我的输出是这样的

Op = {'a':['one','two','three'],'b':['one','five','six'],'d':['five']}

由于它在字典中有一些常见的值,如'one'或'five',因此应该删除重复的值。

例如

- set_fact:
Op: "{{ Op|d({})|combine({item.key: Ip[item.key]}) }}"
loop: "{{ Ip|dict2items }}"
vars:
_lists: "{{ Ip|dict2items|json_query('[].value') }}"
_reduced: "{{ _lists|difference([item.value]) }}"
_match: "{{ _reduced|map('intersect', item.value)|flatten }}"
when: _match|length > 0

Op:
a:
- one
- two
- three
b:
- one
- five
- six
d:
- five

最新更新