4

I have a list that contains multiple sets of strings, and I would like to find the symmetric difference between each string and the other strings in the set.

For example, I have the following list:

targets = [{'B', 'C', 'A'}, {'E', 'C', 'D'}, {'F', 'E', 'D'}]

For the above, desired output is:

[2, 0, 1]

because in the first set, A and B are not found in any of the other sets, for the second set, there are no unique elements to the set, and for the third set, F is not found in any of the other sets.

I thought about approaching this backwards; finding the intersection of each set and subtracting the length of the intersection from the length of the list, but set.intersection(*) does not appear to work on strings, so I'm stuck:

set1 = {'A', 'B', 'C'}
set2 = {'C', 'D', 'E'}
set3 = {'D', 'E', 'F'}

targets = [set1, set2, set3]

>>> set.intersection(*targets)
set()
SummerEla
  • 1,902
  • 3
  • 26
  • 43
  • Is each string a single character? If not, are you going by whether the entire string occurs elsewhere, or just the character by character differences? – Unsolved Cypher Nov 20 '18 at 01:00
  • 1
    Each string contains at least six characters, and I would like to test the entire occurrence of the string. Perhaps I oversimplified my example, but I wasn't sure that would make a difference. – SummerEla Nov 20 '18 at 01:08

3 Answers3

2

The issue you're having is that there are no strings shared by all three sets, so your intersection comes up empty. That's not a string issue, it would work the same with numbers or anything else you can put in a set.

The only way I see to do a global calculation over all the sets, then use that to find the number of unique values in each one is to first count all the values (using collections.Counter), then for each set, count the number of values that showed up only once in the global count.

from collections import Counter

def unique_count(sets):
    count = Counter()
    for s in sets:
        count.update(s)
    return [sum(count[x] == 1 for x in s) for s in sets]
Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • I've found that this answer still works even when there is no difference between sets. @Unsolved Cypher's did not :( – SummerEla Dec 18 '18 at 20:56
1

Try something like below:

Get symmetric difference with every set. Then intersect with the given input set.

def symVal(index,targets):
    bseSet = targets[index] 
    symSet = bseSet  
    for j in range(len(targets)):
        if index != j:
            symSet =  symSet  ^ targets[j] 
    print(len(symSet & bseSet))

for i in range(len(targets)):
    symVal(i,targets)
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
  • 1
    I'm pretty sure this calculation will get the wrong answer if any values are in all three sets (or appear any odd number of times if there are more sets than that). – Blckknght Dec 18 '18 at 21:15
1

Your code example doesn't work because it's finding the intersection between all of the sets, which is 0 (since no element occurs everywhere). You want to find the difference between each set and the union of all other sets. For example:

set1 = {'A', 'B', 'C'}
set2 = {'C', 'D', 'E'}
set3 = {'D', 'E', 'F'}


targets = [set1, set2, set3]

result = []

for set_element in targets:
    result.append(len(set_element.difference(set.union(*[x for x in targets if x is not set_element]))))

print(result)

(note that the [x for x in targets if x != set_element] is just the set of all other sets)

Unsolved Cypher
  • 1,025
  • 10
  • 24
  • Unfortunately, I found an error in your solution. It returns an error when there is no difference between sets. – SummerEla Dec 18 '18 at 20:57
  • 1
    @SummerEla Good catch! I've updated the code, and now it works in that situation as well. I changed the `!=` to an `is not`, so that it's every list is checked against other lists that are different objects rather than just those that have different values (since lists can be different objects but look identical) – Unsolved Cypher Dec 18 '18 at 23:43