2
str1 = ["is" , "first"]       
str2 = ["was" , "second"]          
str3 = "he is doing his first year graduation"       
for i in RANGE(len(str1)):      
str4 = str3.replace(str1[i],str2[i])    

Due to the immutable property only the second change ("first" to "second"). How can i make both changes together. Actually I have a huge size of string. I'm new to python, please help me

4 Answers 4

3

Simply assign the last line to str3:

str1 = ["is" , "first"]       
str2 = ["was" , "second"]          
str3 = "he is doing his first year graduation"       
for i in range(len(str1)):      
    str3 = str3.replace(str1[i],str2[i])  

print str3  # he was doing hwas second year graduation

Pay attention that the is in the word "his" is also being replaced!

If you want an "exact" matching, use the regex module:

import re

str1 = ["is" , "first"]       
str2 = ["was" , "second"]          
str3 = "he is doing his first year graduation"       
for i in range(len(str1)):      
    str3 = re.sub(r'\b{}\b'.format(str1[i]),str2[i], str3)  

print str3  # he was doing his second year graduation
Sign up to request clarification or add additional context in comments.

Comments

1

You could do it in one line by applying reduce and lambda:

ans = reduce(lambda n, (old, new): n.replace(old, new), zip(str1,str2), str3)

example:

>>> str1 = ["is", "first"]
>>> str2 = ["was", "second"]
>>> str3 = "he is doing his first year graduation"
>>> ans = reduce(lambda n, (old, new): n.replace(old, new), zip(str1,str2), str3)
>>> ans
'he was doing hwas second year graduation'

However, I suppose in your origin design "his" would become "hwas", so you might still need to use re.sub as suggested by others:

>>> import re
>>> ans = reduce(lambda n, (old, new): re.sub(r'\b{}\b'.format(old), new, n), zip(str1,str2), str3)
>>> ans
'he was doing his second year graduation'

2 Comments

@Harvey izip() doesn't generate a list, so it should not be able to fit in reduce() function, right?
It returns an iterable which is all that reduce needs. Python3's zip() is actually Python2's itertools.izip(). from itertools import izip; print(reduce(lambda x,y: x + "".join(y), izip("abc","123"), "")) prints a1b2c3
0

an alternative to @alfasin's answer is to enumerate the list, this yields same end result! Also, note that str3 is overwritten in each iteration of the for loop, no str4variable used

str1 = ["is" , "first"]       
str2 = ["was" , "second"]          
str3 = "he is doing his first year graduation"       
for idx, s in enumerate(str1):      
    str3 = str3.replace(s,str2[idx])  

print str3  # he was doing hwas second year graduation

Comments

-1

This is not an issue of immutability. You keep creating a new str4 with only the latest replace. You want to create str4 once, and update it a few times. For example

str1 = ["is", "first"]
str2 = ["was", "second"]
str3 = "he is doing his first year graduation"
str4 = str3
for i in range(len(str1)):
    str4 = str4.replace(str1[i], str2[i])

You'll notice this has a bug where "his" becomes "hwas" (I assume that's a bug). You could use regexes to only match words, but you're likely better off using dictionaries.

Create a dictionary with {str1[i]:str2[i]} for each i. Then iterate through each word in str3 replacing it with its value in the dictionary.

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.