Skip to main content
Fix intersection code
Source Link
Connor
  • 5k
  • 2
  • 34
  • 42

Simple way to find difference and intersection between iterables

Use this method if repetition matters

from collections import Counter

def intersection(a, b):
    """
    Find the intersection of two iterables
    
    >>> intersection((1,2,3), (2,3,4))
    (2, 3)
    
    >>> intersection((1,2,3,3), (2,3,3,4))
    (2, 3, 3)
    
    >>> intersection((1,2,3,3), (2,3,4,4))
    (2, 3)
    
    >>> intersection((1,2,3,3), (2,3,4,4))
    (2, 3)
    """
    return tuple(n for n, count in (Counter(a) & Counter(b)).items() for _ in range(count))

def difference(a, b):
    """
    Find the symmetric difference of two iterables
    
    >>> difference((1,2,3), (2,3,4))
    (1, 4)
    
    >>> difference((1,2,3,3), (2,3,4))
    (1, 3, 4)
    
    >>> difference((1,2,3,3), (2,3,4,4))
    (1, 3, 4, 4)
    """
    diff = lambda x, y: tuple(n for n, count in (Counter(x) - Counter(y)).items() for _ in range(count))
    return diff(a, b) + diff(b, a)

Simple way to find difference and intersection between iterables

Use this method if repetition matters

from collections import Counter

def intersection(a, b):
    """
    Find the intersection of two iterables
    
    >>> intersection((1,2,3), (2,3,4))
    (2, 3)
    
    >>> intersection((1,2,3,3), (2,3,3,4))
    (2, 3, 3)
    
    >>> intersection((1,2,3,3), (2,3,4,4))
    (2, 3)
    """
    return tuple(Counter(a) & Counter(b))

def difference(a, b):
    """
    Find the symmetric difference of two iterables
    
    >>> difference((1,2,3), (2,3,4))
    (1, 4)
    
    >>> difference((1,2,3,3), (2,3,4))
    (1, 3, 4)
    
    >>> difference((1,2,3,3), (2,3,4,4))
    (1, 3, 4, 4)
    """
    diff = lambda x, y: tuple(n for n, count in (Counter(x) - Counter(y)).items() for _ in range(count))
    return diff(a, b) + diff(b, a)

Simple way to find difference and intersection between iterables

Use this method if repetition matters

from collections import Counter

def intersection(a, b):
    """
    Find the intersection of two iterables
    
    >>> intersection((1,2,3), (2,3,4))
    (2, 3)
    
    >>> intersection((1,2,3,3), (2,3,3,4))
    (2, 3, 3)
    
    >>> intersection((1,2,3,3), (2,3,4,4))
    (2, 3)
    
    >>> intersection((1,2,3,3), (2,3,4,4))
    (2, 3)
    """
    return tuple(n for n, count in (Counter(a) & Counter(b)).items() for _ in range(count))

def difference(a, b):
    """
    Find the symmetric difference of two iterables
    
    >>> difference((1,2,3), (2,3,4))
    (1, 4)
    
    >>> difference((1,2,3,3), (2,3,4))
    (1, 3, 4)
    
    >>> difference((1,2,3,3), (2,3,4,4))
    (1, 3, 4, 4)
    """
    diff = lambda x, y: tuple(n for n, count in (Counter(x) - Counter(y)).items() for _ in range(count))
    return diff(a, b) + diff(b, a)
fix the difference calculation and doctests
Source Link
Connor
  • 5k
  • 2
  • 34
  • 42

Simple way to find difference and intersection between iterables

Use this method if repetition matters

from collections import Counter

def intersection(a, b):
    """
    Find the intersection of two iterables
    
    >>> intersection((1,2,3), (2,3,4))
    (2, 3)
    
    >>> intersection((1,2,3,3), (2,3,3,4))
    (2, 3, 3)
    
    >>> intersection((1,2,3,3), (2,3,4,4))
    (2, 3)
    """
    return tuple(Counter(a) & Counter(b))
 

def difference(a, b):
    """
    Find the symmetric difference of two iterables
    
    >>> difference((1,2,3), (2,3,4))
    (1, 4)
    
    >>> difference((1,2,3,3), (2,3,4))
    (1, 3, 4)
    
    >>> difference((1,2, (3,4,5)3), (2,3,4,4))
    (1, (3, 4, 5), 3, 4)
    """
    returndiff = lambda x, y: tuple(n for n, count in (Counter(ax) - Counter(by)).items() +for tuple_ in range(Countercount))
    return diff(a, b) -+ Counterdiff(b, a))

Simple way to find difference between iterables

Use this method if repetition matters

from collections import Counter

def intersection(a, b):
    """
    Find the intersection of two iterables
    
    >>> intersection((1,2,3), (2,3,4))
    (2, 3)
    
    >>> intersection((1,2,3,3), (2,3,3,4))
    (2, 3, 3)
    """
    return tuple(Counter(a) & Counter(b))
 

def difference(a, b):
    """
    Find the difference of two iterables
    
    >>> difference((1,2,3), (2,3,4))
    (1, 4)
    
    >>> difference((1,2,3,3), (2,3,4))
    (1, 3, 4)

    >>> difference((1,2, (3,4,5)), (2,3,4))
    (1, (3, 4, 5), 3, 4)
    """
    return tuple(Counter(a) - Counter(b)) + tuple(Counter(b) - Counter(a))

Simple way to find difference and intersection between iterables

Use this method if repetition matters

from collections import Counter

def intersection(a, b):
    """
    Find the intersection of two iterables
    
    >>> intersection((1,2,3), (2,3,4))
    (2, 3)
    
    >>> intersection((1,2,3,3), (2,3,3,4))
    (2, 3, 3)
    
    >>> intersection((1,2,3,3), (2,3,4,4))
    (2, 3)
    """
    return tuple(Counter(a) & Counter(b))

def difference(a, b):
    """
    Find the symmetric difference of two iterables
    
    >>> difference((1,2,3), (2,3,4))
    (1, 4)
    
    >>> difference((1,2,3,3), (2,3,4))
    (1, 3, 4)
    
    >>> difference((1,2,3,3), (2,3,4,4))
    (1, 3, 4, 4)
    """
    diff = lambda x, y: tuple(n for n, count in (Counter(x) - Counter(y)).items() for _ in range(count))
    return diff(a, b) + diff(b, a)
Source Link
Connor
  • 5k
  • 2
  • 34
  • 42

Simple way to find difference between iterables

Use this method if repetition matters

from collections import Counter

def intersection(a, b):
    """
    Find the intersection of two iterables
    
    >>> intersection((1,2,3), (2,3,4))
    (2, 3)
    
    >>> intersection((1,2,3,3), (2,3,3,4))
    (2, 3, 3)
    """
    return tuple(Counter(a) & Counter(b))


def difference(a, b):
    """
    Find the difference of two iterables
    
    >>> difference((1,2,3), (2,3,4))
    (1, 4)
    
    >>> difference((1,2,3,3), (2,3,4))
    (1, 3, 4)

    >>> difference((1,2, (3,4,5)), (2,3,4))
    (1, (3, 4, 5), 3, 4)
    """
    return tuple(Counter(a) - Counter(b)) + tuple(Counter(b) - Counter(a))