hello guys... i have a bit of a problem with my assignment im working on :(

what the problem is there is a function which accepts a string, a keyword and an array of keywords to replace the keyword occurrences with

for eg, def function(text, keyword, keyword_list):

and for eg, the text that is passed might have an occurance of TEST in there 4 times ('TEST TEST TEST TEST'). and the keyword_list might be "". and it needs to replace the first instance of TEST in the string with the first keyword (keyword_list[0]) in the list. second with the second (keyword_list[1]) and all remaining instances with (keyword_list[2])

so the outcome from 'TEST TEST TEST TEST' would be 'TEST_1 TEST_2 TEST_3 TEST_3'

i have no idea where to start with it :? string.find() and string.replace() wont work as it will replace all instances of TEST with one swift go. and i only want to replace the first.

thanks in advance! im really stuck :(

I wondered why this has been asked many many times in the last few days both here and on python-forum. At least you are honest enough to admit that it is homework. You should be able to find this asked and answered many times already. If you do want help, you'll have to make an effort and post some code to be critiqued.

I think this might have a lot to do with the competition being held in Australia for High School Students in python at the moment, it sound suspiciously like a problem that is stumping almost all of the people on the final week.

I have to admit, this is a clever problem. With a little thought it shouldn't stomp anybody. Here are some hints ...

# assume you have the following

text = """\
My name is OUR_HERO, or simply OUR_HERO, somtimes OUR_HERO.
You, my dear friend, can call me OUR_HERO.
"""

keyword_list = ['James Bond', 'Bond', '007']

keyword = 'OUR_HERO'

# count keyword in the text
n = text.count(keyword)

Once you have the count n, you can set up a loop and replace the keyword consecutively with elements in the keyword_list using something like ...

# replace keyword in text s with selected key_list item
# 1 consecutive item each time
s = s.replace(keyword, keyword_list[k], 1)

You have to set up the loop for k and make sure you that you don't run past the index. Mind you, that replace may not work properly with all possible keywords. All-capital keywords should help a little. One could use the re module, but that may be beyond the scope. Well, enough hinting. If you are stuck, do some test printing.

The problem they are running into is replacing "the" with "xxx" for example, but not finding and replacing "then" with "xxxn". I would suggest using split() to get the words, only replacing exact matches, and join() to reconstruct the sentence. If there are punctuation marks like commas in the phrase then that also has to be taken into account. But you are 90% there from the last two posts and that is enough.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.