Python3中的条件合并

时间:2013-08-23 21:47:25

标签: python dictionary python-3.x merge

我正在尝试将变量合并到字典中存在的键中。 以下是规则:

  
      
  1. 如果密钥已存在,则将值计数器递增1。
  2.   
  3. 如果存在与键的部分匹配:   一个。如果变量length小于key但re.search不是None,则将值计数器递增1   湾如果变量长度大于key且re.search不是None,则将key替换为变量并将计数器增加一个
  4.   
  5. 如果此后存在变量但在字典中仍然没有匹配,则将变量添加到字典
  6.   

我已经能够完成1,2a和2b,但我不知道如何添加3.任何帮助/建议将不胜感激。 这是目前形式的剧本:我也希望在Dict中看到“乌龟”。

Animals = ["phant", "eleph", "tiger", "turtle", "zebra", "ostrich"]
Dict = {"horse":1, "elephant":1, "iger":1, "ostrich":1}

for name in Animals:
     if name in Dict:
             Dict[name]=Dict[name]+1
     else:
             for key, val in Dict.items():
                     if len(name) < len(key):
                             m = re.search (name, key)
                             if m != None:
                                     print ("Found match!", name)
                                     Dict[key] = Dict[key] + 1
                                     break
                     elif len(name) > len(key):
                              m = re.search (key, name)
                              if m != None:
                                     print ("Found match!", name)
                                     Dict[name] = Dict.pop(key) + 1
                                     Dict[name] = Dict[name] + 1
                                     break

2 个答案:

答案 0 :(得分:1)

在查看代码后描述案例#3的一种方法是“如果break语句都没有执行”。您可以通过在else循环后放置for语句来处理这种情况。只有在for循环运行完成时(或换句话说,循环内没有break语句运行),才会执行此块中的代码:

Animals = ["phant", "eleph", "tiger", "turtle", "zebra", "ostrich"]
Dict = {"horse":1, "elephant":1, "iger":1, "ostrich":1}

for name in Animals:
    if name in Dict:
        Dict[name]=Dict[name]+1
    else:
        for key, val in Dict.items():
            if len(name) < len(key):
                m = re.search (name, key)
                if m != None:
                    print ("Found match!", name)
                    Dict[key] = Dict[key] + 1
                    break
            elif len(name) > len(key):
                m = re.search (key, name)
                if m != None:
                    print ("Found match!", name)
                    Dict[name] = Dict.pop(key) + 1
                    Dict[name] = Dict[name] + 1
                    break
        else:                 # this line and the one below it are new
            Dict[name] = 1

答案 1 :(得分:0)

for name in Animals:
    if name in Dict:
        Dict[name]=Dict[name]+1
    else:
        found= False
        for key, val in Dict.items():
            if len(name) < len(key):
                m = re.search (name, key)
                if m != None:
                    print ("Found match!", name)
                    Dict[key] = Dict[key] + 1
                    found= True
                    break
            elif len(name) > len(key):
                m = re.search (key, name)
                if m != None:
                    print ("Found match!", name)
                    Dict[name] = Dict.pop(key) + 1
                    found= True
                    break
        if not found
            Dict[name] = 1

我更喜欢以下内容:

for name in Animals:
    if name in Dict:
        Dict[name]=Dict[name]+1
    else:
        found= False
        for key, val in Dict.items():
            m = re.search (name, key)
            if m != None:
                print ("Found match!", name)
                if len(name) < len(key):
                    Dict[key] = Dict[key] + 1
                else
                    Dict[name] = Dict.pop(key) + 1
                found= True
                break
        if not found
            Dict[name] = 1