嗨,我想弄清楚如何比较我拥有的两个不同哈希映射的值。
hash1 = {'animals':['dogs','cats']}
hash2 = {'canine': ['dogs','wolves']}
从上面的示例中,由于 hash2 中的键 canine 具有与 hash1 中也具有 'dogs' 的键动物匹配的值 'dogs',我希望它打印出 'canine'。
当一个键只有一个值时,我能够做这样的事情,但我需要它有一个长长的值列表,如果任何值匹配,我希望它打印出它与哪个键匹配。
编辑:我希望它打印出'canine',因为例如,如果我在 hash2 中有多个键
hash2 = {'canine':['dogs','wolves'],'domestic':['horse','rabbit']}
我只希望它打印出 'canine' 因为那是匹配的,而不是打印出整个 hash2
编辑 2: hash1 = {'animals':['dogs','cats']} hash2 = {'canine': ['dogs','wolves']}
for value in hash2.values():
if value in hash1.values():
#not sure how to write this so here's pseudocode
print(hash2[key of matching value])