简体   繁体   English

将一本词典的键与另一本词典的键与值列表进行比较

[英]Comparing keys of one dictionary to another dictionary with a list of values

I have a dictionary with a list of values of varying numbers of items. 我有一本字典,其中列出了数量不等的项目值。 I want to compare the values of this dictionary (dict1) to the keys of another (dict2), and if they match print the key and value of the matching component of dict1, along with the value of dict2. 我想将此字典(dict1)的值与另一个字典(dict2)的键进行比较,如果它们匹配,则打印dict1的匹配组件的键和值以及dict2的值。 Both of the dictionaries are quite large, and currently this is taking way too long as you could have guessed from this basic script. 这两个字典都非常大,目前正在进行中,因为您可能已经从这个基本脚本中猜到了。

dict1 = {boys:[tom,jon],girls:[suzy]}

dict2 = {suzy:paper-stapler-extraordinaire,jon:paper-shredderoligist,tom:garbage-specialist}

output: 输出:

    boys \t tom \t garbage-specialist

    boys \t jon \t paper-shredderoligist  etc.....

for k,v in dict2.items():

    for key,value in dict1.items():
         if k in value[0]:
             print str(key)+"\t"+str(value[0])+"\t"+v
         if len(value)>1:
             if k in value[1]:
                 print str(key)+"\t"+str(value[0])+"\t"+v

Could someone suggest a more memory efficient method? 有人可以提出一种内存效率更高的方法吗? Perhaps a list comprehension? 也许列表理解? This has not been working... a = [k for k in dict2 if k in dict] 这没有起作用... a = [如果dict中的k为dict2中的k,则k为dict2]

for dict1_key, dict1_values in dict1.iteritems():
    for dict1_value in dict1_values:
        try:
            dict2_value = dict2[dict1_value]
            print str(dict1_key) + '\t' + str(dict1_value) + '\t' + str(dict2_value)
        except KeyError:
            pass

That combines a few techniques to speed it up and use less memory. 这结合了一些技术来加快速度并使用更少的内存。 iteritems uses less memory (as others have mentioned). iteritems使用较少的内存(如其他人所述)。 By using try and then using dict2[dict1_value] , you can ensure that dict2 is only searched once, and then with the hash algorithm, which should be much faster than iterating through all the elements. 通过使用try然后再使用dict2[dict1_value] ,可以确保dict2进行一次搜索,然后使用哈希算法,该算法比遍历所有元素的速度要快得多。 For all the cases where dict2 does not have dict1_value in it, the first statement under try fails, causing the catch block to harmlessly pass . 对于dict2中没有dict1_value所有情况, dict2下的第一条语句都会失败,从而使catch块无害地pass

Are you looking for something along the lines of: 您是否正在寻找符合以下条件的产品:

[(k,i,dict2[i]) for k,v in dict1.items() for i in v if i in set(dict2.keys())]

which returns the key,value of dict1 and value of dic2 for every value in dict1 that is a key in dict2. 对于dict2中作为键的每个值,返回dict1的键,值dic1和dic2的值。 This can be edited to return a string etc... 可以对其进行编辑以返回字符串等。

This outputs: 输出:

[('boys', 'tom', 'garbage-specialist'),
 ('boys', 'jon', 'paper-shredderoligist'),
 ('girls', 'suzy', 'paper-stapler-extraordinaire')]

The set() on dict2.keys() is used to make the key look-up faster. dict2.keys()上的set()用于使键查找更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM