繁体   English   中英

如何按空格分割字符串的字符,然后按特殊字符和数字分割列表的结果元素,然后再次加入它们?

[英]How to split the characters of a string by spaces and then resultant elements of list by special characters and numbers and then again join them?

所以,我想要做的是将字符串中的一些单词转换为字典中各自的单词并保持原样。例如,通过将输入作为:

standarisationn("well-2-34 2   @$%23beach bend com")

我希望输出为:

"well-2-34 2 @$%23bch bnd com"

我使用的代码是:

def standarisationn(addr):
a=re.sub(',', ' ', addr)
lookp_dict = {"allee":"ale","alley":"ale","ally":"ale","aly":"ale",
              "arcade":"arc",
               "apartment":"apt","aprtmnt":"apt","aptmnt":"apt",
               "av":"ave","aven":"ave","avenu":"ave","avenue":"ave","avn":"ave","avnue":"ave",
              "beach":"bch",
              "bend":"bnd",
              "blfs":"blf","bluf":"blf","bluff":"blf","bluffs":"blf",
              "boul":"blvd","boulevard":"blvd","boulv":"blvd",
              "bottm":"bot","bottom":"bot",
              "branch":"br","brnch":"br",
              "brdge":"brg","bridge":"brg",
              "bypa":"byp","bypas":"byp","bypass":"byp","byps":"byp",
              "camp":"cmp",
              "canyn":"cny","canyon":"cny","cnyn":"cny",
              "southwest":"sw" ,"northwest":"nw"}

temp=re.findall(r"[A-Za-z0-9]+|\S", a)
print(temp)
res = []
for wrd in temp:
     res.append(lookp_dict.get(wrd,wrd))
res = ' '.join(res)
return str(res) 

但它给出了错误的输出:

'well - 2 - 34 2 @ $ % 23beach bnd com'

那是有太多空格,甚至没有将“海滩”转换为“bch”。所以,这就是问题所在。我认为首先将字符串按空格拆分,然后按特殊字符和数字拆分结果元素,然后使用字典,然后首先用没有空格的特殊字符连接分隔的字符串,然后用空格连接所有列表。谁能建议如何解决这个问题或任何更好的方法?

您可以使用字典的键构建正则表达式,确保它们不包含在另一个单词中(即不直接在前面或后面跟一个字母):

import re
def standarisationn(addr):
    addr = re.sub(r'(,|\s+)', " ", addr)
    lookp_dict = {"allee":"ale","alley":"ale","ally":"ale","aly":"ale",
                "arcade":"arc",
                "apartment":"apt","aprtmnt":"apt","aptmnt":"apt",
                "av":"ave","aven":"ave","avenu":"ave","avenue":"ave","avn":"ave","avnue":"ave",
                "beach":"bch",
                "bend":"bnd",
                "blfs":"blf","bluf":"blf","bluff":"blf","bluffs":"blf",
                "boul":"blvd","boulevard":"blvd","boulv":"blvd",
                "bottm":"bot","bottom":"bot",
                "branch":"br","brnch":"br",
                "brdge":"brg","bridge":"brg",
                "bypa":"byp","bypas":"byp","bypass":"byp","byps":"byp",
                "camp":"cmp",
                "canyn":"cny","canyon":"cny","cnyn":"cny",
                "southwest":"sw" ,"northwest":"nw"}

    for wrd in lookp_dict:
        addr = re.sub(rf'(?<=[^a-zA-Z]){wrd}(?=[^a-zA-Z])', lookp_dict[wrd], addr)
    return addr

print(standarisationn("well-2-34 2   @$%23beach bend com"))

该表达式由三部分构成:

  • (?<=[^a-zA-Z])是后视(即非捕获表达式),检查前面的字符是否是字母
  • {wrd}是你字典的键
  • (?=[^a-zA-Z])是前瞻(即非捕获表达式),检查后面的字符是否是字母

输出:

well-2-34 2 @$%23bch bnd com

编辑:如果将循环替换为以下内容,则可以编译整个表达式并仅使用 re.sub 一次:

repl_pattern = re.compile(rf"(?<=[^a-zA-Z])({'|'.join(lookp_dict.keys())})(?=[^a-zA-Z])")
addr = re.sub(repl_pattern, lambda x: lookp_dict[x.group(1)], addr)

如果您的字典增长,这应该会快得多,因为我们使用您的所有字典键构建了一个表达式:

  • ({'|'.join(lookp_dict.keys())})被解释为(allee|alley|...
  • re.sub 中的 lambda 函数用 lookp_dict 中的相应值替换匹配元素(例如,请参阅此链接以获取更多详细信息)

暂无
暂无

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

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