我需要在地址字段中替换类似于“north”、“south”等的文本为“N”、“S”等。我想过用字典来保存要替换的内容。假设我们有:
replacements = {'NORTH':'N','SOUTH':'S','EAST':'E','WEST':'W'}
address = "123 north anywhere street"
我可以使用replacements
字典来进行所有替换,例如通过迭代它吗?这样做的代码是什么样子?
我需要在地址字段中替换类似于“north”、“south”等的文本为“N”、“S”等。我想过用字典来保存要替换的内容。假设我们有:
replacements = {'NORTH':'N','SOUTH':'S','EAST':'E','WEST':'W'}
address = "123 north anywhere street"
我可以使用replacements
字典来进行所有替换,例如通过迭代它吗?这样做的代码是什么样子?
所有这些答案都很好,但是你错过了Python字符串替换 - 它简单而快速,但需要正确格式化你的字符串。
address = "123 %(direction)s anywhere street"
print(address % {"direction": "N"})
'score: 99.5% name:%(name)s' %{'name':'foo'}
。 - ahuigo%
样式占位符,则我们也可以直接编辑替换内容。 - Karl Knechteltoken_mapping = {
'north': 'N', 'south': 'S',
'east': 'E', 'west': 'W'
'street': 'St',
}
def tokenize(text):
return text.lower().split()
def detokenize(tokens):
return ' '.join(tokens)
def replace_tokens(text, token_mapping=token_mapping):
input_tokens = tokenize(text)
output_tokens = []
for tok in input_tokens:
output_tokens.append(token_mapping.get(tok, tok))
return detokenize(output_tokens)
>>> replace_tokens("123 north anywhere street")
'123 N anywhere St'
def detokenize(tokens):
return ' '.join([t.title() for t in tokens])
>>> replace_tokens("123 north anywhere street")
'123 N Anywhere St'
import re
root_synonyms = {'NORTH':'N','SOUTH':'S','EAST':'E','WEST':'W'}
# put the longest search term first. This menas the system does not replace "top" before "tank top"
synonym_keys = sorted(root_synonyms.keys(),key=len,reverse=True)
# the groups will be named w1, w2, ... . Determine what each of them should become
number_mapping = {f'w{i}':root_synonyms[key] for i,key in enumerate(synonym_keys) }
# make a regex for each word where "tank top" or "tank top" are the same
search_terms = [re.sub(r'\s+',r'\s+',re.escape(k)) for k in synonym_keys]
# give each search term a name w1 etc where
search_terms = [f'(?P<w{i}>\\b{key}\\b)' for i,key in enumerate(search_terms)]
# make one huge regex
search_terms = '|'.join(search_terms)
# compile it for speed
search_re = re.compile(search_terms,re.IGNORECASE)
query = "123 north anywhere street"
result = re.sub(search_re,lambda x: number_mapping[x.lastgroup],query)
print(result)
replace()
方法会返回一个替换了出现次数的字符串的副本 - 它不会原地进行替换。 - martineau