2

I implemented a version of the str_replace function available in php using python. Here is my original code that didn't work

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

but this prints out

hello world
???

the code i got to do as expected is

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

which prints out

 hello world
 h???? w?r?d

just like I wanted it to.

Aside from the fact that there is probably a way using builtins that i haven't seen yet, why does the first way fail and the second way do what I need it to?

1
  • I hate it when a simple cosmetic change is invisible to me and causes my things to not work right but I forget about the change i made! Commented Feb 14, 2010 at 4:47

3 Answers 3

4

The ordering of the arguments to replacer is what makes the difference between the two. If you changed the argument ordering in the first version it'd behave like the second version.

Sign up to request clarification or add additional context in comments.

Comments

3

Don't use built-in names such as str for your own identifiers, that's just asking for trouble and has no benefit whatsoever.

Apart from that, your first version is looping on str, the second argument -- the list ['e', 'l', 'o'] -- so of course it will return a string of exactly three items -- how could you expect it to return a string of any other length?! Using str to name a list argument is particularly perverse and bug-prone.

The second version loops on str, the first argument -- the string 'hello world' so of course it's returning a string of that length.

3 Comments

i know it was just a quick example i was trying to show one of my friends who does a lot of php
is there a way to do multiple replacement like this with the builtin string methods or is a small piece of code like this what I need each time?
@controlfreak, the .translate method of strings is much better (but for bytestrings in Python 2.* you have to prep a translate table with string.maketrans first -- Unicode objects, and Python 3 strings which are also unicode, are neater as they take a dict).
1

You're passing it in backwards in the first one. It should be should be

test = replacer(['e','l','o'], test, '?')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.