如何获取与同一词典中的另一个键共享值的键列表? [英] How to get list of keys that share a value with another key within the same dictionary?
本文介绍了如何获取与同一词典中的另一个键共享值的键列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个唯一键字典,其中某些键共享相同的值.
I have a dictionary of unique keys where some keys share the same value.
例如:
D = {'ida':{'key':'1'},'idb':{'key':'2'},'idc':{'key':'3'},'idd':{'key':'3'},'ide':{'key':'4'},'idf':{'key':'4'},'idg':{'key':'4'}}
我想要一个与其他键共享相同值的键列表.
I want a list of keys that share the same value with other keys.
在这种情况下,应该是
l = ['idc','idd','ide','idf','idg']
但是,我想从共享相同值的所有键集中排除一个键.
However, I want to exclude one key from all sets of keys that share the same value.
例如,我想要按键
l = ['idd','idf','idg']
其中不包括"idc"和"ide"
which excludes 'idc' and 'ide'
或者可能是
l = ['idc','ide','idf']
,其中不包括"idd"和"idg".
which excludes 'idd' and 'idg'.
推荐答案
如果该值多次出现,列表组合将添加键.
If the value appears more than once the list comp will add the key.
dup_keys = [k for k in D if sum(D[k] in x for x in D.iteritems()) > 1 ]
['idf', 'idg', 'idd', 'ide', 'idc']
dup_keys[1:]
['idg', 'idd', 'ide', 'idc']
dup_keys[1:-1]
['idg', 'idd', 'ide']
如果sum(D.k中的D [k]等于D.iteritems()中的x),1
检查值是否出现>1
次.
要忽略某些键,请添加一些和
条件,但我不确定要在什么基础上忽略键.
To ignore certain keys add some and
condition, I am not sure on what basis you want to ignore the keys.
这篇关于如何获取与同一词典中的另一个键共享值的键列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文