(n)
Return tuple used in permutation and combination counting. Input
    is a dictionary giving items with counts as values or a sequence of
    items (which need not be sorted).

    The data is stored in a class deriving from tuple so it is easily
    recognized and so it can be converted easily to a list.
Return tuple used in permutation and combination counting. Input
    is a dictionary giving items with counts as values or a sequence of
    items (which need not be sorted).
Return tuple used in permutation and combination counting . Input is a dictionary giving items with counts as values or a sequence of items ( which need not be sorted ) .
def _multiset_histogram(n):
    """Return tuple used in permutation and combination counting. Input
    is a dictionary giving items with counts as values or a sequence of
    items (which need not be sorted).

    The data is stored in a class deriving from tuple so it is easily
    recognized and so it can be converted easily to a list.
    """
    if type(n) is dict:  # item: count
        if not all(isinstance(v, int) and v >= 0 for v in n.values()):
            raise ValueError
        tot = sum(n.values())
        items = sum(1 for k in n if n[k] > 0)
        return _MultisetHistogram([n[k] for k in n if n[k] > 0] + [items, tot])
    else:
        n = list(n)
        s = set(n)
        if len(s) == len(n):
            n = [1]*len(n)
            n.extend([len(n), len(n)])
            return _MultisetHistogram(n)
        m = dict(zip(s, range(len(s))))
        d = dict(zip(range(len(s)), [0]*len(s)))
        for i in n:
            d[m[i]] += 1
        return _multiset_histogram(d)
def _multiset_histogram ( n ) : if type ( n ) is dict : # item: count if not all ( isinstance ( v , int ) and v >= 0 for v in n . values ( ) ) : raise ValueError tot = sum ( n . values ( ) ) items = sum ( 1 for k in n if n [ k ] > 0 ) return _MultisetHistogram ( [ n [ k ] for k in n if n [ k ] > 0 ] + [ items , tot ] ) else : n = list ( n ) s = set ( n ) if len ( s ) == len ( n ) : n = [ 1 ] * len ( n ) n . extend ( [ len ( n ) , len ( n ) ] ) return _MultisetHistogram ( n ) m = dict ( zip ( s , range ( len ( s ) ) ) ) d = dict ( zip ( range ( len ( s ) ) , [ 0 ] * len ( s ) ) ) for i in n : d [ m [ i ] ] += 1 return _multiset_histogram ( d )