蟒蛇映射我实现了一个版本使用python在PHP中提供的str_replace函数的功能。这是我原来的代码没有工作用绳子
def replacer(items,str,repl):
return "".join(map(lambda x:repl if x in items else x,str))
test = "hello world"
print test
test = replacer(test,['e','l','o'],'?')
print test
但这打印出
hello world
???
我得做预期的代码是
def replacer(str,items,repl):
x = "".join(map(lambda x:repl if x in items else x,str))
return x
test = "hello world"
print test
test = replacer(test,['e','l','o'],'?')
print test
打印出
hello world
h???? w?r?d
就像我想它牛逼O操作。
除了一个事实,即有可能是使用我还没有看到内建的方式,为什么第一种方式失败,第二种方式做我需要它?
我讨厌它,当一个简单的外观上的改变是不可见的我,导致我的东西不工作的权利,但我忘了我所做的改变! – 2010-02-14 04:47:44