-2

Python.

   Str1="acddeffg" 
   Str2="fgfdeca" 

I want to obtain the letter/s that are in Str1 not present in Str2 and viceversa.

In this example is Np1="", Np2="d"

    Str1="abcdefffg"
    Str2="aabcdef"

Answer Np1="a", Np2="ffg"

furas
  • 134,197
  • 12
  • 106
  • 148
user83877
  • 1
  • 1

2 Answers2

1

Re-reading your question, you probably want to do this:

str1 = "abcdefffg"
str2 = "aabcdef"

def find_diff(left, right):

    l = list(left)
    for letter in right:
        if letter in l:
            l.remove(letter)
    
    return "".join(l)
    
print(find_diff(str1, str2))  # ffg
print(find_diff(str2, str1))  # a

Original answer: You could use sets to get the difference between used letters (ignoring duplicates). Not what the OP is looking for (second example) but I will leave it here for reference.

letters1 = set("abcdefffg")
letters2 = set("aabcdef")

print(letters1 - letters2)  # {'g'}
print(letters2 - letters1)  # set()
Thomas
  • 8,357
  • 15
  • 45
  • 81
0

You can try this:

Np1 = ''.join([letter for letter in Str1 if letter not in Str2])

Np2 = ''.join([letter for letter in Str2 if letter not in Str1])
Omar
  • 1
  • 2
  • That's no works, because I need to obtain "ffg" and "a. repetitions must be taken into account. With yor method I obtain "g" and "" – user83877 Oct 04 '21 at 00:55