【发布时间】:2021-10-26 11:59:23
【问题描述】:
所以,我想要做的是将字符串中的一些单词转换为字典中它们各自的单词并保持原样。例如,输入如下:
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”。所以,这就是问题所在。我认为是先用空格分割字符串,然后用特殊字符和数字分割结果元素,然后使用字典,然后首先用不带空格的特殊字符连接分隔的字符串,然后用空格连接所有列表。有人可以建议如何解决这个问题或任何更好的方法吗?
【问题讨论】:
标签: python dictionary join split python-re