【发布时间】:2012-09-17 19:43:15
【问题描述】:
我在 Python 中有一个字符串,比如The quick @red fox jumps over the @lame brown dog.
我正在尝试将每个以 @ 开头的单词替换为将单词作为参数的函数的输出。
def my_replace(match):
return match + str(match.index('e'))
#Psuedo-code
string = "The quick @red fox jumps over the @lame brown dog."
string.replace('@%match', my_replace(match))
# Result
"The quick @red2 fox jumps over the @lame4 brown dog."
有没有聪明的方法来做到这一点?
【问题讨论】:
-
你所拥有的是好的。你在一个声明中做到这一点。