14

I'm looking for the way of comparing two strings and being able to get back, as separate strings:

  • All the common characters,
  • The uncommon characters, (all characters but without the common ones)
  • Characters that are unique to one string.

Example:

A = "123 ABC"
B = "135 AZ"

thingamajigger(A, B)  # would give all these:

intersect = "13 A"  # (includes space)
exclusion = "2BCZ5"
a_minus_b = "2BC"
b_minus_a = "5Z"

a_minus_b is quite simple... but if there's one of those fancy one-liner ways to pull it off, then I'm open.

for i in B:
    A = A.replace(i, "")

It's a bit like boolean operations on strings.

1 Answer 1

15

Use set:

s = set("123 ABC")
t = set("135 AZ")
intersect = s & t # or s.intersection(t)
exclusion = s ^ t # or s.symmetric_difference(t)
a_minus_b = s - t # or s.difference(t)
b_minus_a = t - s # or t.difference(s)
4
  • Wow... of course... and then you can just use "".joint(set) to squash it into a string! Thanks.
    – Jollywatt
    Commented Jul 16, 2013 at 2:36
  • @Joseph: It is beautiful, isn't it? :-)
    – jason
    Commented Jul 16, 2013 at 2:54
  • 2
    The only thins is, sets only contain unique elements. So, say I wanted to subtract "!" from "Hello!". Using set("Hello!") - set("!") would give "Helo". But using the alternative a_minus_b above works. :)
    – Jollywatt
    Commented Jul 16, 2013 at 3:02
  • 1
    @Joseph: I see. If that's what you want, I'd use "".join([c for c in A if c not in set(B)]) though; it's vastly more efficient (additive in the lengths vs. multiplicative in the lengths).
    – jason
    Commented Jul 16, 2013 at 3:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.