Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查Python字典值是否在另一个字典中';s值_Python_Loops_For Loop_Dictionary - Fatal编程技术网

检查Python字典值是否在另一个字典中';s值

检查Python字典值是否在另一个字典中';s值,python,loops,for-loop,dictionary,Python,Loops,For Loop,Dictionary,我有两个python字典,如果字典键之间没有共同的值,我想返回字典键的组合。 有点像: 因为我在‘a’: 对于“b”中的j: 对于i.items(): 如果在任何.j.items()中有.i.items() 继续 否则,请告诉我如何将所有组合组合组合在一起 假设我的代码如下所示: a={'dog':['hungry','fun'],'cat':['ugly','old'],'fish':[1,2,'hungry',4]} b={'raccoon':['ugly',1,3,], 'bird':[1

我有两个python字典,如果字典键之间没有共同的值,我想返回字典键的组合。 有点像:

  • 因为我在‘a’:
  • 对于“b”中的j:
  • 对于i.items():
  • 如果在任何.j.items()中有.i.items()
  • 继续
  • 否则,请告诉我如何将所有组合组合组合在一起
  • 假设我的代码如下所示:

    a={'dog':['hungry','fun'],'cat':['ugly','old'],'fish':[1,2,'hungry',4]}
    b={'raccoon':['ugly',1,3,], 'bird':[18,'hungry'],'cat':['orange','short']}
    
    这会回来的

    dog+raccoon, dog+cat, cat+bird, cat+cat, fish+cat 
    

    因为这些潜在的配对都没有共同的字典值。如果有人能告诉我该如何做,我将不胜感激。

    也许这是我能得到的:

    a={'dog':['hungry','fun'],'cat':['ugly','old'],'fish':[1,2,'hungry',4]}
    b={'raccoon':['ugly',1,3,], 'bird':[18,'hungry'],'cat':['orange','short']}
    
    result = []
    for b_key, b_value in b.iteritems():
        for a_key, a_value in a.iteritems():
            if not any(x in a_value for x in b_value):
                result.append((a_key, b_key))
    print(result)
    [('dog', 'raccoon'), ('cat', 'bird'), ('fish', 'cat'), ('dog', 'cat'), ('cat', 'cat')]
    
    可以使用以下集合:

    from itertools import product
    pairs = set()
    for (ak, av), (bk, bv) in product(a.items(), b.items()):
        if not set(av).intersection(set(bv)):
            pairs.add("%s+%s" % tuple(sorted([ak, bk])))
    print pairs
    
    产生:

    如果您首先使用集合而不是列表,那么这会更有效

    def no_shared(a,b):
        res = []
        pairs = [(x, y) for x in a for y in b]
        for x,y in pairs:
            if set(a[x]) & set(b[y]):
                continue
            res.append((x, y))
        return res
    
    基本上测试列表(作为集合)之间的任何交集,如果它们有交集,则跳过添加

    此外,如果列表值对于该dict中的条目总是唯一的(从示例中看似乎如此),那么将其最初存储为一个集合可以节省转换

    def no_shared(a,b):
        res = []
        pairs = [(x, y) for x in a for y in b]
        for x,y in pairs:
            if set(a[x]) & set(b[y]):
                continue
            res.append((x, y))
        return res