控制for循环的流程

时间:2013-04-30 05:52:21

标签: python

我正在编写一个伪代码,我想要重复下面的两个“for”循环,直到data_changes和modem_changes中的所有键都相同,即不应该有任何键存在data_changes而不是modem_changes和版本反之亦然。我应该能够在此之后编写Python实现;谁能提供投入? 编辑:

更感兴趣
  1. 如何为一个而不是另一个
  2. 中存在的键调用func1和func2

    2.重复for循环,直到data_changes和modem_changes中的键相同

    data_changes = {
        '253036': [''], 
        '313115': ['313113']
        }
    
    modem_changes = {'305403': [], 
                     '311957': ['253036', '312591']
                     }
    
    s1 = set(data_changes.keys())
    s2 = set(modem_changes.keys())
    value1 = s2.difference(s1)
    print value1
    value2 = s1.difference(s2)
    print value2
    
    def func1 (data_changes,key):
        if key == '311957':
            output = ''
        if key == '305403':
            output = ''
            return output
    
    def func2 (modem_changes,key):
        if key == '313115':
            output =''
        if key == '253036':
            output=''
        return output
    
    def method(d1, f1, d2, f2):
        s1 = set(d1.keys())
        s2 = set(d2.keys())
        for k in s2.difference(s1):#set(['311957', '305403'])
            f1(d1, k) # k in d2 not in d1.
        for k in s1.difference(s2):#set(['313115', '253036'])
            f2(d2, k) # k in d1 not in d2.
    
    while(True):
        method(data_changes, func1, modem_changes, func2)
        value = set(data_changes.keys()) - set(modem_changes.keys()) 
        print value
        if value == set([]):
            break;
    
    
    EXPECTED OUTPUT:-
    
    data_changes = {
        '253036': [''], 
        '313115': ['313113']
        '305403':[''] 
        '311957':['']
        }
    
    modem_changes = {'305403': [], 
                     '311957': ['253036', '312591']
                     '253036':[]
                     '313115':[] 
                     }
    

3 个答案:

答案 0 :(得分:1)

In [8]: keys = set(data_changes.keys()) & set(modem_changes.keys())

In [9]: data_changes = {k:data_changes[k] for k in keys}

In [10]: modem_changes = {k:modem_changes[k] for k in keys}

In [11]: data_changes
Out[11]: {'253036': ['']}

In [12]: modem_changes
Out[12]: {'253036': ['311957', '312994', '312591']}

答案 1 :(得分:0)

尝试以下操作以继续重复for循环:

while(True):
    for key in data_changes:
        if key not in modem_changes:
            func1()
    for key in modem_changes:
        if key not in data_changes:
            func2()
    if(True): #logic to check wether to run for loops again
        break;

答案 2 :(得分:0)

由于您没有通过“密钥相同”来解释您的确切含义,我将假设func1func2是您已编写的函数,并在我的答案中使用它们。我还假设函数采用他们正在处理的字典/密钥。如果不是这种情况,您可以随时在代码中进行更改。

def method(d1, f1, d2, f2):
    s1 = set(d1.keys())
    s2 = set(d2.keys())
    for k in s2.difference(s1):
        f1(d1, k) # k in d2 not in d1.
    for k in s1.difference(s2):
        f2(d2, k) # k in d1 not in d2.

上述方法采用以下形式的参数,例如:

method(data_changes, func1, modem_changes, func2)

如果您的意思是删除所有不在两个词典中的键,则以下代码段之一应该为您执行:

def keep_similar(d1, d2):
    '''creates new dicts from shared keys'''
    shared_keys = set(d1.keys()).intersection(set(d2.keys()))
    r1 = dict((k, d1[k]) for k in shared_keys)
    r2 = dict((k, d2[k]) for k in shared_keys)
    return (r1, r2)

def eliminate_diffs(d1, d2):
    '''removes non-shared keys from dicts'''
    s1 = set(d1.keys())
    s2 = set(d2.keys())
    for k in s2.difference(s1):
        del d2[k]
    for k in s1.difference(s2):
        del d1[k]
    return (d1, d2) 

只需选择最适合您情况的那个,如果您的钥匙很少,那么使用第一个,如果你有几个不相似的钥匙,那么使用第二个。