Python-如何在字典中获取具有相同值的键/

Python-如何在字典中获取具有相同值的键/,python,dictionary,Python,Dictionary,我有一句格言: {'Key_1': ['Value_1'], 'Key_2': ['Value_1', 'Value_2'], 'Key_3': ['Value_2'], 'Key_4': ['Value_3']} 我希望获得具有相同值的键,例如,输出如下: Key_1 and Key_2 have same Value_1 Key_2 and Key_3 have same Value_2 我尝试这样做是为了得到共同的价值观: list_1 = [] output = [] for val

我有一句格言:

{'Key_1': ['Value_1'], 'Key_2': ['Value_1', 'Value_2'], 'Key_3': ['Value_2'], 'Key_4': ['Value_3']}
我希望获得具有相同值的键,例如,输出如下:

Key_1 and Key_2 have same Value_1
Key_2 and Key_3 have same Value_2
我尝试这样做是为了得到共同的价值观:

list_1 = []
output = []
for value in dictionary.values():
    for x in value:
         if x in list_1:
            if not x in output:
                output.append(x)
         else:
             list_1.append(x)
通过这个,我得到了公共值,但没有得到相应的键

提前谢谢你

d = {'Key_1': ['Value_1'], 'Key_2': ['Value_1', 'Value_2'], 'Key_3': ['Value_2'], 'Key_4': ['Value_3']}

out = {}
for k, v in d.items():
    for vv in v:
        out.setdefault(vv, []).append(k)

for k, v in out.items():
    if len(v) > 1:
        print('{} have same {}'.format(' and '.join(v), k))
印刷品:

Key_1 and Key_2 have same Value_1
Key_2 and Key_3 have same Value_2
印刷品:

Key_1 and Key_2 have same Value_1
Key_2 and Key_3 have same Value_2

你有没有试过做一个dictoanary,但是用key作为值,value作为键你不能总是这样做,因为dict键必须是可散列的,而列表是不可散列的。你有没有试过做一个dictoanary,但是用key作为值,value作为键你不能总是这样做,因为dict键必须是可散列的,而列表是不可散列的。