(m, n, g=None)
Return the unique combinations of size ``n`` from multiset ``m``.

    Examples
    ========

    >>> from sympy.utilities.iterables import multiset_combinations
    >>> from itertools import combinations
    >>> [''.join(i) for i in  multiset_combinations('baby', 3)]
    ['abb', 'aby', 'bby']

    >>> def count(f, s): return len(list(f(s, 3)))

    The number of combinations depends on the number of letters; the
    number of unique combinations depends on how the letters are
    repeated.

    >>> s1 = 'abracadabra'
    >>> s2 = 'banana tree'
    >>> count(combinations, s1), count(multiset_combinations, s1)
    (165, 23)
    >>> count(combinations, s2), count(multiset_combinations, s2)
    (165, 54)
Return the unique combinations of size ``n`` from multiset ``m``.
Return the unique combinations of size n from multiset m .
def multiset_combinations(m, n, g=None):
    """
    Return the unique combinations of size ``n`` from multiset ``m``.

    Examples
    ========

    >>> from sympy.utilities.iterables import multiset_combinations
    >>> from itertools import combinations
    >>> [''.join(i) for i in  multiset_combinations('baby', 3)]
    ['abb', 'aby', 'bby']

    >>> def count(f, s): return len(list(f(s, 3)))

    The number of combinations depends on the number of letters; the
    number of unique combinations depends on how the letters are
    repeated.

    >>> s1 = 'abracadabra'
    >>> s2 = 'banana tree'
    >>> count(combinations, s1), count(multiset_combinations, s1)
    (165, 23)
    >>> count(combinations, s2), count(multiset_combinations, s2)
    (165, 54)

    """
    if g is None:
        if type(m) is dict:
            if n > sum(m.values()):
                return
            g = [[k, m[k]] for k in ordered(m)]
        else:
            m = list(m)
            if n > len(m):
                return
            try:
                m = multiset(m)
                g = [(k, m[k]) for k in ordered(m)]
            except TypeError:
                m = list(ordered(m))
                g = [list(i) for i in group(m, multiple=False)]
        del m
    if sum(v for k, v in g) < n or not n:
        yield []
    else:
        for i, (k, v) in enumerate(g):
            if v >= n:
                yield [k]*n
                v = n - 1
            for v in range(min(n, v), 0, -1):
                for j in multiset_combinations(None, n - v, g[i + 1:]):
                    rv = [k]*v + j
                    if len(rv) == n:
                        yield rv
def multiset_combinations ( m , n , g = None ) : if g is None : if type ( m ) is dict : if n > sum ( m . values ( ) ) : return g = [ [ k , m [ k ] ] for k in ordered ( m ) ] else : m = list ( m ) if n > len ( m ) : return try : m = multiset ( m ) g = [ ( k , m [ k ] ) for k in ordered ( m ) ] except TypeError : m = list ( ordered ( m ) ) g = [ list ( i ) for i in group ( m , multiple = False ) ] del m if sum ( v for k , v in g ) < n or not n : yield [ ] else : for i , ( k , v ) in enumerate ( g ) : if v >= n : yield [ k ] * n v = n - 1 for v in range ( min ( n , v ) , 0 , - 1 ) : for j in multiset_combinations ( None , n - v , g [ i + 1 : ] ) : rv = [ k ] * v + j if len ( rv ) == n : yield rv