Replacing characters from string one to string two

I need to create a function that takes parameters (character, stringOne, stringTwo). The function returns a new word that contains the same data as stringTwo + any characters in stringOne at the same position.

Example:

  • stringOne = "apple"
  • stringTwo = "12345"
  • character = "p"
  • Must return "1pp45"

I lost how I should act.

def replace(char, word1, word2): newWord = "" for s in range(len(word1)): if word1[s] == char: return 
+5
source share
6 answers

Here's how to modify existing code so that it does what you want. I changed newWord to new_word according to the standard Python style convention.

 def replace(char, word1, word2): new_word = "" for i in range(len(word2)): if word1[i] == char: new_word += char else: new_word += word2[i] return new_word # Test print(replace("p", "apple", "12345")) 

Output

 1pp45 

Note that this code will raise an IndexError: string index out of range exception IndexError: string index out of range if word1 shorter than word2 .

There are various ways to implement this function. First, we can use the zip function to iterate the characters of both lines in parallel. This is a little cleaner than your indirect iteration, using string indices to access characters.

 def replace(char, word1, word2): new_word = "" for c1, c2 in zip(word1, word2): if c1 == char: new_word += char else: new_word += c2 return new_word 

Note that if the lines are the same length, then the zip stop gracefully when the shorter line ends.

We can make this code more compact by replacing the if... else block with a conditional expression.

 def replace(char, word1, word2): new_word = "" for c1, c2 in zip(word1, word2): new_word += char if c1 == char else c2 return new_word 

A more advanced version uses list comprehension to create a list of desired characters and a .join string .join to convert that list to a string.

 def replace(char, word1, word2): return ''.join([char if c1 == char else c2 for c1, c2 in zip(word1, word2)]) 

This can also be done using a generator expression.

 ''.join(char if c1 == char else c2 for c1, c2 in zip(word1, word2)) 

However, this is less effective than using list comprehension because .join works. .join should double-check its argument: the first scan is used to calculate the total size of the output string, the second scan is the actual connection. But you cannot scan the generator twice, so if you pass .join generator, it must create a temporary list from the generator.

+2
source

Continuing what you did using a simple for-loop and triple operation, here's how to do it:

 def replace(char, word1, word2): newWord = "" for s in range(len(word1)): newWord += char if word1[s] == char else word2[s] return newWord 

Which is equivalent:

 def replace(char, word1, word2): newWord = "" for s in range(len(word1)): if word1[s] == char: newWord += char else: newWord += word2[s] return newWord 

Just in case, you are not familiar with the triple operation.

+5
source

You can also use list comprehension, repeat and replace.

 >>> x, y, c = 'apple', '12345', 'p' >>> ''.join([c if x[i] == c else s for i, s in enumerate(y)]) '1pp45' 
+1
source

The pythonic way:

 >>> stringOne = "apple" >>> stringTwo = "12345" >>> my_char = "p" >>> "".join([x[1] if my_char!=x[0] else x[0] for x in zip(stringOne, stringTwo)]) '1pp45' 
+1
source
 def replace(char, word1, word2): newWord= list(word2) for counter in range(min(len(word1), len(word2))): if word1[counter]== char: newWord[counter]= char return "".join(newWord) 
+1
source

The simplest solution would be to iterate through the characters of the word1, and if a match of the characters is found with the character that we are interested in searching, then we replace the character with this index in the word2.

 def replace(char, word1, word2): newWord = "" word2List = list(word2) # Check is character is present in word1 # and if length of word1 and word2 are equal if char not in word1 or len(word1) != len(word2): return False else: for i,c in enumerate(word1): if c == char: word2List[i] = c # Converting list back to string return ''.join(word2List) print(replace('p','apple','12345')) 

Output:

 1pp45 
0
source

Source: https://habr.com/ru/post/1244407/


All Articles