Python String Handling
Jon Ribbens
jon+usenet at unequivocal.eu
Sat Nov 12 08:02:02 EST 2016
On 2016-11-12, subhabangalore at gmail.com <subhabangalore at gmail.com> wrote:
> I am restating the problem.
>
> "Hello my name is Richard"
>
> is a string.
>
> I have tagged the words Hello and Richard
> as "Hello/Hi" and "Richard/P".
> After this I could get the string as a list of words
> as in,
> ['Hello/Hi','my','name','is','Richard/P']
>
> Now I want to replace the string with
> Hello/Hi my name is Richard/P
>
> It may seem a joining of list but is not because
> if I try to make,
> ['Hello/Hi','my/M','name','is/I','Richard/P']
>
> I may do, but doing the following string
>
> Hello/Hi my/M<HM> name is/I Richard/P<IP>
>
> is tough as entities with tag may vary. I have to make
> a rule.
>
> I am trying to recognize the index of the word in
> the list, pop it and replace with new value and joining
> the list as string.
I'm afraid your description of the problem is still rather... opaque.
>From your original post it looked like the answer would be:
def replacer(target, replacements):
for replacement in replacements:
if "/" in replacement:
target = target.replace(*replacement.split("/", 1))
return target
print(replacer("Hello my name is Richard",
['Hello/Hi','my','name','is','Richard/P']))
but in your new description where you start talking about word indices
I have no idea if that's what you actually wanted.
More information about the Python-list
mailing list