0

I want to delete all the letters in two strings that are not the same between them.

For example:

str1 = 'abcdefg'

str2 = 'efghijk'

After removing the different characters I want to get:

str1 = 'efg'

str2 = 'efg'

I have the following code:

for i in str1:
    if i not in str2:
        str1 = str1.replace("i", "")  #delete that element
                                      #strings are immutable so I create a new string and remove that element

Then I would do the same with the other string. However, when I print it out it does not delete all the elements I want it to. Or sometimes it doesn't delete any of the elements.
I have also tried it with a double for loop but it does not work correctly. Is there something wrong with my logic?

aa1
  • 783
  • 1
  • 15
  • 31
  • 2
    I believe you are replacing the literal letter `i` with a blank. You aren't using the variable that you called `i`, you are using the string `"i"`. – takendarkk Jun 22 '18 at 15:08
  • Based on this, https://stackoverflow.com/questions/3559559/how-to-delete-a-character-from-a-string-using-python, I wanted to delete that element – aa1 Jun 22 '18 at 15:09
  • @ce1 but you are replacing the actual string `"i"`. You should use `i`, not `"i"` – DeepSpace Jun 22 '18 at 15:10
  • 1
    You code says "find the letter i and replace it with a blank". You don't want to "find the letter i", you want to "find the letter that the variable i holds". – takendarkk Jun 22 '18 at 15:11
  • Yes that makes sense! that was my mistake. thank you! – aa1 Jun 22 '18 at 15:12

4 Answers4

2

You can define the common elements as follows

common = set(str1) & set(str2)

Then you can simply filter your strings not to include those elements as

str1 = ''.join([i for i in str1 if i in common])
str2 = ''.join([i for i in str2 if i in common])
kosnik
  • 2,342
  • 10
  • 23
  • Couldn't we get the same result without first having to find the common elements ?` `str1 = ''.join([i for i in str1 if i in str2])` then `str2 = ''.join([i for i in str2 if i in str1])` We'll first keep the elements of str1 that are also in str2 (common elements), then use said found common elements to change str2 in the desired way. – HolyDanna Jun 22 '18 at 15:28
  • 2
    That's true, we can get the same result in this way. If however we have more strings this approach becomes cumbersome. Also, if the strings are long, then checking if element is in a `set` is much faster O(1), in contrast to checking if it exists in a string. – kosnik Jun 22 '18 at 15:44
1

I would just use a simple list comprehension and then assign the elements to a string

new_str = ''.join([i for i in str1 if i in str2])

Your error is here:

str1 = str1.replace("i", "")

should be

str1 = str1.replace(i, "")

Take note, this solution, being quadratic in the input size, is relatively inefficient and should not be used for large strings.

Eli Korvigo
  • 10,265
  • 6
  • 47
  • 73
APorter1031
  • 2,107
  • 6
  • 17
  • 38
  • @EliKorvigo the question is not concerned about efficiency. If this answer does not supply a valid solution then a down vote would be just. – APorter1031 Jun 22 '18 at 15:26
1

Another approach (if you don't care about the order) is to use sets:

>>> str1 = 'abcdefg'
>>> str2 = 'efghijk'
>>> common = ''.join(set(str1) & set(str2))
>>> common
'feg'
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
0

You can use sets even if you do care about order. Just sort it:

>>> 
>>> str1 = 'abcdefg'
>>> str2 = 'efghijk'
>>> ''.join(sorted(set(str1) & set(str2), key = str1.index))
'efg'
>>> ''.join(set(str1) & set(str2))
'egf'
>>> str2 = 'egfhijk'
>>> ''.join(set(str1) & set(str2))
'egf'
>>> ''.join(sorted(set(str1) & set(str2), key = str1.index))
'efg'
>>> 
shrewmouse
  • 5,338
  • 3
  • 38
  • 43