获取具有匹配值的两个词典的键

时间:2014-05-06 02:40:25

标签: python dictionary

我想通过使用python查找等效键,或者更确切地说具有相同值的键来比较两个词典。我试图做/找到两件事:

  1. 两个字典中具有相同值的键,以及
  2. 在其他词典中没有等效键的键。
  3. 我的词典看起来像这样:

    dict_1={
    '3471': ['AY219713', 'AJ504663'], '3460': ['AJ621556', 'AJ575744'], '0': ['AM158981', 'AM158980', 'AM158982', 'AM158979', 'AY594216', 'AY594215', 'EU053207', 'AM392286', 'L26168', 'L37584']}
    
    dict_2 = {'478': ['AY219713', 'AJ504663'], '43': ['AJ575744', 'AJ621556'], '2321': ['AM158979', 'L37584', 'AM158982', 'EU053207', 'AY594215', 'AM392286', 'AY594216', 'L26168', 'AM158981', 'AM158980', 'CP000758', 'AY776289', 'AJ242581', 'AM422370', 'U70978', 'AY457038', 'FR668302', 'AM422371', 'AM490632', 'AM490617', 'AJ242584']}
    

    所以我想尝试获得看起来像这样的输出,

    Matching keys from Dictionary 1 and Dictionary 2:
    3471 = 478, 2 values in common
    3460 = 43, 2 values in common
    
    Keys with no equal match:
    Dictionary 1 = 0, etc.
    Dictionary 2 = 2321, etc.
    

    具有不同价值观的常用密钥并不重要。

4 个答案:

答案 0 :(得分:2)

这是aj8uppal答案的一个细微变化 - 这对于它所谓的匹配和不匹配更为清晰,我认为结果更接近你所要求的。承认它部分受到aj8uppal的启发;请注意,为了便于阅读,我缩短了dict_1dict_2,我只对它们进行了一次排序(当你有5000个密钥时很重要......):

dict_1 = {
'3471': ['AY219713', 'AJ504663'],
'3460': ['AJ621556', 'AJ575744'],
'0': ['AM158981', 'AM158980', 'AM158982']}

dict_2 = {
'478': ['AY219713', 'AJ504663'],
'43': ['AJ575744', 'AJ621556'],
'2321': ['AM158979', 'L37584', 'AM158982']}

match1 = {}
match2 = {}
# sort once: makes a difference when you have lots of elements
for k in dict_1:
  dict_1[k] = sorted(dict_1[k])
for j in dict_2:
  dict_2[j] = sorted(dict_2[j])
for k in dict_1:
  for j in dict_2:
    if (dict_1[k] == dict_2[j]):
      match1[k]=1
      match2[j]=1
      break;

print "matching keys:"
print list(match1)
print list(match2)
print "\nnon matching keys:"
print list(set(dict_1.keys()) - set(match1))
print list(set(dict_2.keys()) - set(match2))

这给出了以下输出:

matching keys:
['3471', '3460']
['478', '43']

non matching keys:
['0']
['2321']

答案 1 :(得分:1)

使用以下代码:

dict_1={
'3471': ['AY219713', 'AJ504663'], '3460': ['AJ621556', 'AJ575744'], '0': ['AM158981', 'AM158980', 'AM158982', 'AM158979', 'AY594216', 'AY594215', 'EU053207', 'AM392286', 'L26168', 'L37584']}

dict_2 = {'478': ['AY219713', 'AJ504663'], '43': ['AJ575744', 'AJ621556'], '2321': ['AM158979', 'L37584', 'AM158982', 'EU053207', 'AY594215', 'AM392286', 'AY594216', 'L26168', 'AM158981', 'AM158980', 'CP000758', 'AY776289', 'AJ242581', 'AM422370', 'U70978', 'AY457038', 'FR668302', 'AM42237
for k in dict_1:
    for i in dict_2:
        if sorted(dict_1[k]) == sorted(dict_2[i]):
            matches.append((k, i))

for k in matches:
    print '%s = %s' %(k[0], k[1])

运行如下:

bash-3.2$ python sortkey.py
3471 = 478
3460 = 43
bash-3.2$

答案 2 :(得分:1)

使用sets代替sorted list

进行一些愚蠢的列表理解
for x in [i[0] for i in filter(None,[["%s = %s" %(key1,key2) for key1 in dict_1.keys() if set(dict_1[key1]) == set(dict_2[key2])] for key2 in dict_2.keys()])]:
    print x

答案 3 :(得分:1)

这可以通过反转字典并查看一组键(原始词典的值)来实现。

dict_1 = {
'3471': ['AY219713', 'AJ504663'],
'3460': ['AJ621556', 'AJ575744'],
'0': ['AM158981', 'AM158980', 'AM158982']}

dict_2 = {
'478': ['AY219713', 'AJ504663'],
'43': ['AJ575744', 'AJ621556'],
'2321': ['AM158979', 'L37584', 'AM158982']}

def compare_dicts(D1, D2):
    dict_1 = {str(sorted(v)): k for k, v in D1.iteritems()}
    dict_2 = {str(sorted(v)): k for k, v in D2.iteritems()}
    KEYS  = set(dict_1.keys() + dict_2.keys())
    both = []
    just = {0:[], 1:[]}
    for k in KEYS:
        Vals = dict_1.get(k, False), dict_2.get(k, False)
        if all(map(bool, Vals)):
            both.append('{} = {}'.format(*map(str, Vals)))
        else:
            i = 0 if Vals[0] else 1
            just[i].append(Vals[i])

    print 'Matching keys from Dictionary 1 and Dictionary 2:'
    print '\n'.join(both)
    print
    print 'Keys with no equal match:'
    print 'Dictionary 1 = {}'.format(','.join(map(str, just[0])))
    print 'Dictionary 2 = {}'.format(','.join(map(str, just[1])))


compare_dicts(dict_1, dict_2) 

这提供了所需的

输出
Matching keys from Dictionary 1 and Dictionary 2:
3460 = 43
3471 = 478

Keys with no equal match:
Dictionary 1 = 0
Dictionary 2 = 2321