对于我的dict中的每个键,我希望有独特的值: 我目前的实施:
from collections import defaultdict
a = ["key1", "key2"]
b = ["value1", "value2", "value2", "value3"]
d = defaultdict(list)
for key in a:
for value in b:
d[key].append(value)
目前的结果是:
{'key1': ["value1", "value2", "value2", "value3"] , 'key2': ["value1", "value2", "value2", "value3"]}
但我想(即使是在混乱中):
{'key1': ["value1", "value2", "value3"], 'key2': ["value1", "value2", "value3"]}
(每个键的值都是唯一的)
答案 0 :(得分:0)
尝试使用字典值集:
d = defaultdict(set)
for key in a:
for value in b:
d[key].add(value)
请注意,这将要求值可以清除。