我有一套相当简单的要求。我有一个对象列表(长度为200万),每个对象有两个需要重新执行的属性(其他属性不变)。
零一二的值…需要将10改为其数值:12…十
示例:
ONE MAIN STREET -> 1 MAIN STREET
BONE ROAD -> BONE ROAD
BUILDING TWO, THREE MAIN ROAD -> BUILDING 2, 3 MAIN ROAD
ELEVEN MAIN ST -> ELEVEN MAIN STREET
ONE HUNDRED FUNTOWN -> 1 HUNDRED FUNTOWN
很明显,有些数字是不变的,有些是奇怪的。
那是完全可以预料的
我可以利用下面的内容来完成所有工作。我的问题是,有没有一个聪明的方法让这一切运行得更快?我想做一个
list
属于
dictionaries
其中键是单词数字,值是数字,但我认为这对性能没有帮助。或
re.compile
每个regex并将它们传递给这个函数?有什么聪明的主意能让这个跑得更快吗?
def update_word_to_numeric(entrylist):
updated_entrylist = []
for theentry in entrylist:
theentry.addr_ln_1 = re.sub(r"\bZERO\b", "0", theentry.addr_ln_1)
theentry.addr_ln_1 = re.sub(r"\bONE\b", "1", theentry.addr_ln_1)
theentry.addr_ln_1 = re.sub(r"\bTWO\b", "2", theentry.addr_ln_1)
theentry.addr_ln_1 = re.sub(r"\bTHREE\b", "3", theentry.addr_ln_1)
theentry.addr_ln_1 = re.sub(r"\bFOUR\b", "4", theentry.addr_ln_1)
theentry.addr_ln_1 = re.sub(r"\bFIVE\b", "5", theentry.addr_ln_1)
theentry.addr_ln_1 = re.sub(r"\bSIX\b", "6", theentry.addr_ln_1)
theentry.addr_ln_1 = re.sub(r"\bSEVEN\b", "7", theentry.addr_ln_1)
theentry.addr_ln_1 = re.sub(r"\bEIGHT\b", "8", theentry.addr_ln_1)
theentry.addr_ln_1 = re.sub(r"\bNINE\b", "9", theentry.addr_ln_1)
theentry.addr_ln_1 = re.sub(r"\bTEN\b", "10", theentry.addr_ln_1)
theentry.addr_ln_2 = re.sub(r"\bZERO\b", "0", theentry.addr_ln_2)
theentry.addr_ln_2 = re.sub(r"\bONE\b", "1", theentry.addr_ln_2)
theentry.addr_ln_2 = re.sub(r"\bTWO\b", "2", theentry.addr_ln_2)
theentry.addr_ln_2 = re.sub(r"\bTHREE\b", "3", theentry.addr_ln_2)
theentry.addr_ln_2 = re.sub(r"\bFOUR\b", "4", theentry.addr_ln_2)
theentry.addr_ln_2 = re.sub(r"\bFIVE\b", "5", theentry.addr_ln_2)
theentry.addr_ln_2 = re.sub(r"\bSIX\b", "6", theentry.addr_ln_2)
theentry.addr_ln_2 = re.sub(r"\bSEVEN\b", "7", theentry.addr_ln_2)
theentry.addr_ln_2 = re.sub(r"\bEIGHT\b", "8", theentry.addr_ln_2)
theentry.addr_ln_2 = re.sub(r"\bNINE\b", "9", theentry.addr_ln_2)
theentry.addr_ln_2 = re.sub(r"\bTEN\b", "10", theentry.addr_ln_2)
updated_entrylist.append(theentry)
return updated_entrylist
也许这只是一个很好的方法。“够好了”的评论对我也很好:)