蟒蛇映射我實現了一個版本使用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