1

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

  1. 如果键已经存在,则将值计数器加一。
  2. 如果存在与键的部分匹配:如果变量长度小于 key 但 re.search 不是 None,则将 value 计数器加一 b。如果 variable length 大于 key 并且 re.search 不是 None,则用 variable 替换 key 并将 counter 加一
  3. 如果变量在此之后存在但在字典中仍然没有匹配,则将变量添加到字典

我已经能够完成 1、2a 和 2b,但我不确定如何添加 3。任何帮助/建议将不胜感激。这是目前形式的脚本:我还想在字典中看到“乌龟”。

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
4

2 回答 2

1

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

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
于 2013-08-23T21:54:03.177 回答
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
于 2013-08-23T21:53:34.243 回答