2

Basically, I want to remove the characters from two strings, as many times as they occur in both. For example if we got two strings "cat","cats", when I print the final result I would have "s". What have I done is the following: I have a function which takes two strings and converts them into two separate lists, which are sorted straight after that. I've got a loop which checks whether the first part of the string is in the second one and then it removes the occurrence, however the problem is that if I use the strings "cat","cast" I would get "st" instead of just "s". I know where the problem is exactly in my code. I believe I have to loop through both of the lists but I can't find a solution.I am using python 3 Thank you for taking your time.

def func1(arg1,arg2)
  l1=list(arg1)
  l1.sort()
  l2=list(arg2)
  l2.sort()
    for i in l1:
    if (l1[0]==l2[0]):
            del l2[0]
  • Possible duplicate of [How can I iterate through two lists in parallel in Python?](http://stackoverflow.com/questions/1663807/how-can-i-iterate-through-two-lists-in-parallel-in-python) – juanpa.arrivillaga Oct 25 '16 at 19:23
  • That was the first result of googling "python loop through both of the lists". Please check if your question has already been answered before posting a new one. It's not possible to look through every question, but a basic google search isn't too much to ask. – juanpa.arrivillaga Oct 25 '16 at 19:24
  • Also, an additional issue is that you are modifying a list while iterating through it. That is asking for bugs unless you are very careful. – juanpa.arrivillaga Oct 25 '16 at 19:25
  • for the `cast, cat` example, do you want `s` or `st`? – Patrick Haugh Oct 25 '16 at 19:38
  • what do you expect to happen with `"catcat","cats"` ? Are the arguments sets (unique value lists). or just strings ? – corn3lius Oct 25 '16 at 19:56

3 Answers3

1

You can convert each of the strings to sets and take their symmetric_difference:

def func1(arg1, arg2):
    diff = set(arg1).symmetric_difference(arg2)
    return ''.join(sorted(diff))

>>> func1('cat', 'cast')
's'
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
0

The problem with the code you've written is that it depends on the position in the string. You're checking if the character at a given index in arg1 is the same as the character in the same position in arg2. You want to check for all occurrences of the character, regardless of position. A way of writing that solution independent of position might be

def foo(arg1, arg2):
    new_string = arg2
    for char in set(arg1):
        new_string = new_string.replace(char, "")
    for char in set(arg2):
        new_string = new_string.replace(char, "")
    return new_string

Depending on the desired result, can use the symmetric difference of the sets for each string. That would be elements in either arg1 or arg2, but not both.

>>> set("cat") ^ set("cast")
{"s"}
>>> set("hello") ^ set("world")
{'d', 'e', 'h', 'w', 'r'}

Or if you want the regular difference, use difference between arg2 and arg1. That would be elements in arg2, excluding elements in arg1.

>>> set("cast") - set("cat")
{'s'}
>>> set("world") - set("hello")
{'r', 'd', 'w'}
sytech
  • 29,298
  • 3
  • 45
  • 86
  • i think your approach wouldn't catch `"cats" ^ "casts"` – corn3lius Oct 25 '16 at 19:49
  • @corn3lius Not sure what you mean. Both those strings contain the same characters. Therefore, there should be no characters in the result. That's the correct result when testing the symmetric difference of the sets of those two strings; an empty set. – sytech Oct 25 '16 at 19:51
  • you're right in a set but not in a one for one comparison of the strings. I asked the OP for more clarification. – corn3lius Oct 25 '16 at 20:00
  • Yeah, I provided a few variations because there might be some ambiguity in what OP needs. – sytech Oct 25 '16 at 20:45
0

this will check all the values of the shorter string and remove duplicates or adding missing values returning the differences of them .

I believe the set approach wouldn't catch "catt" ^ "cats" = ['t','s']

def func1(arg1,arg2):

    l1, l2 = list(arg1), list(arg2)
    if len(l1) > len(l2):
        l2,l1 = l1,l2
    while l1: 
        val= l1.pop()
        if l2.count(val) > 0:
            l2.remove(val)
        else:
            l2.extend(val)
    return l2
corn3lius
  • 4,857
  • 2
  • 31
  • 36