我正在寻找以下不言自明的代码的pythonic和有效的替代品:
term = "< << >" # turn this into "( < )"
term.replace("<<", "#~").replace(">>", "~#").replace(">", ")").replace("<", "(").replace("#~", "<").replace("~#", ">")
有任何想法吗?
使用正则表达式:
import re
d = {'<': '(', '>': ')'}
replaceFunc = lambda m: m.group('a') or d[m.group('b')]
pattern = r"((?P<a><|>)(?P=a)|(?P<b><|>))"
term = "< << >"
replaced = re.sub(pattern, replaceFunc, term) #returns "( < )"
根据 Niklas B 的建议进行编辑。
上面的正则表达式等价于匹配:
("<<" OR ">>") OR ("<" OR ">")
(?P<a><|>) #tells the re to match either "<" or ">", place the result in group 'a'
(?P=a) #tells the re to match one more of whatever was matched in the group 'a'
(?P<b><|>) #tells the re to match a sing "<" or ">" and place it in group 'b'
实际上,lambda 函数replaceFunc
只是重复这个匹配序列,但返回相关的替换字符。
这re
匹配“最大组优先”,因此"<<< >"
将转换为"<( )"
.
这是比我的第一个答案更短的方法。它拆分双重字符序列上的输入以删除它们,然后将这些段与替换的单个字符再次连接起来。和以前一样,它使用字典来指定应该进行的替换。
def convert(s, replacements):
for before, after in replacements.items():
s = before.join([segment.replace(before, after) for segment in s.split(before + before)])
return s
>>> convert('< << >', {'<': '(', '>': ')'})
'( < )'
我将所有替换术语放在一个列表中,然后迭代并替换:
CHARS = [
('<<', '#~'),
('>>', '~#'),
...
]
for replace in CHARS:
term = term.replace(*replace)
不确定它是否是最pythonic,但似乎很清楚。您甚至可以考虑接收字符列表的 forloop。
我不确定这是 Pythonic,但它可以工作并且非常灵活。
def convert(s, replacements):
pending_char = None
result = []
for c in s:
if c in replacements:
if c == pending_char:
result.append(c)
pending_char = None
else:
pending_char = c
else:
if pending_char:
result.append(replacements[pending_char])
pending_char = None
result.append(c)
if pending_char:
result.append(replacements[pending_char])
return ''.join(result)
>>> convert('< << >', {'<': '(', '>': ')'})
'( < )'