0

i have this code:

word = ["General William Shelton, said the system",
        "which will provide more precise positional data",
        "and that newer technology will provide more",
        "Commander of the Air Force Space Command",
        "objects and would become the most accurate metadata"]

i wan to replace:

Replace “the” with “THE”, “
Replace “William Shelton” with “AliBaba”
Replace “data” with “SAMSUNG”

the output should be:

 General AliBaba,said THE system which will provide more precise
 positional SAMSUNG and that newer technology will provide more
 Commander of the Air Force Space Command objects and would become the
 most accurate metadata

Thank you!

I have tried this:

rep_word = {"the":"THE", "William Shelton":"AliBaba", "system":"Samsung"}
replace = re.compile(r'\b(' + '|'.join(rep_word.keys()) + r')\b')
result = replace.sub(lambda x: rep_word[x.group()], word)
print result

but i got this error : TypeError: expected string or buffer

2
  • 1
    Please show us what have you tried so far ? Commented Aug 19, 2013 at 9:12
  • i just got an error.. i had edited in the question. Commented Aug 19, 2013 at 9:15

4 Answers 4

1

I think you can use python built function "reduce":

def change(prev, s):
     ret = s.replace("the", "THE")
     ret = ret.replace("William Shelton","AliBaba")
     ret = ret.replace("data", "SAMSUNG")
     return prev+' '+ret

reduce(change, word, '')
Sign up to request clarification or add additional context in comments.

3 Comments

then how to print it out?
@wanmohdpayed X = reduce(change, word, '') and then you can print the variable content: print X
Note that word 'metadata' in source text was not changed, but your code will replace it with "metaSAMSUNG".
1
import re
word = ["General William Shelton, said the system",
        "which will provide more precise positional data",
        "and that newer technology will provide more",
        "Commander of the Air Force Space Command",
        "objects and would become the most accurate metadata"]
replacements = [("the", "THE"), ("William Shelton", "AliBaba"), ("data", "SAMSUNG")]
compiled = [(re.compile(r'\b%s\b' % re.escape(s)), d) for s, d in replacements]
replaced = [reduce(lambda s,(regex,res): regex.sub(lambda _: res, s), compiled, w) for w in word]
result = ' '.join(replaced)
result
'General AliBaba, said THE system which will provide more precise positional SAMSUNG and that newer technology will provide more Commander of THE Air Force Space Command objects and would become THE most accurate metadata'

Comments

0

x is a string. And use x.replace("X","Y"). (Replace X with Y).

Comments

0

Try looking into Link Comprehensions

word = [words.replace('the','THE') for words in word];

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.