0

I have two lists with tuples as shown below

a=[("a","b","c"),("d","e","f"),("h","e","d")]
b=[("b","c","a"),("d","e","f")]

I want to get the difference between two lists efficiently considering that the order of the elements in the tuple do not matter. So set(a) - set(b) does not work, which gives me [('a', 'b', 'c'), ('h', 'e', 'd')] as output.

Instead, I want the output given below. It should detect tuples as the same if the elements are just shuffled, such as (a, b, c) and (b, a, c).

[('h', 'e', 'd')]
busybear
  • 10,194
  • 1
  • 25
  • 42
Nikhil Dhirmalani
  • 127
  • 1
  • 1
  • 7

2 Answers2

2

You can use sets and come back to list:

>>> list(set([tuple(sorted(x)) for x in a]) - set([tuple(sorted(x)) for x in b]))
[('d', 'e', 'h')]

I order tuples to exclude tuples with same letters but in different order.

dani herrera
  • 48,760
  • 8
  • 117
  • 177
2

This post gives you an idea of how to check for equality, based on your definition. Using this, you can loop through items in a to see if they match any in b.

from collections import Counter

diff = []
for x in a:
    if not any(Counter(x) == Counter(y) for y in b):
        diff.append(x)

An alternative is to sort your tuples then use set difference:

a = [tuple(sorted(x)) for x in a]
b = [tuple(sorted(x)) for x in b]

diff = set(a) - set(b)
busybear
  • 10,194
  • 1
  • 25
  • 42
  • 1
    I like your first one approach: `[ x for x in a if all(Counter(x) != Counter(y) for y in b)]` or closer to natural language `[ x for x in a if not any(Counter(x)==Counter(y) for y in b)]` – dani herrera Feb 09 '19 at 21:41
  • @daniherrera The "natural" one is a little easier to read actually. Can't decide if it's easier to read all smashed in a list comprehension though! – busybear Feb 09 '19 at 21:46
  • 1
    Ha Ha. You changed it! My comment was just a little suggestion. Anyway, I guess your `Counter` approach is better than the `sorted` solution I also used in my answer. By the way, `sorted(list(x))` is the same than `sorted(x)` – dani herrera Feb 09 '19 at 21:52
  • Oh you're right. I was thinking of the method `sort` which tuples don't have. – busybear Feb 09 '19 at 22:02