def entropy_bits(
        lst: Union[
            List[Union[int, str, float, complex]],
            Tuple[Union[int, str, float, complex]]
        ]
) -> float:
    """Calculate the number of entropy bits in a list or tuple of elements."""
    # Based on https://stackoverflow.com/a/45091961
    if not isinstance(lst, (list, tuple)):
        raise TypeError('lst must be a list or a tuple')

    for num in lst:
        if not isinstance(num, (int, str, float, complex)):
            raise TypeError('lst can only be comprised of int, str, float, '
                            'complex')

    n_lst = len(lst)

    if n_lst <= 1:
        return 0.0

    # Some NumPy replacements
    counts = [lst.count(x) for x in lst]
    probs = [c / n_lst for c in counts]

    # Compute entropy
    entropy = 0.0
    for prob in probs:
        entropy -= prob * log2(prob)

    return entropy
def entropy_bits ( lst : Union [ List [ Union [ int , str , float , complex ] ] , Tuple [ Union [ int , str , float , complex ] ] ] ) -> float : if not isinstance ( lst , ( list , tuple ) ) : raise TypeError ( 'lst must be a list or a tuple' ) for num in lst : if not isinstance ( num , ( int , str , float , complex ) ) : raise TypeError ( 'lst can only be comprised of int, str, float, ' 'complex' ) n_lst = len ( lst ) if n_lst <= 1 : return 0.0 counts = [ lst . count ( x ) for x in lst ] probs = [ c / n_lst for c in counts ] entropy = 0.0 for prob in probs : entropy -= prob * log2 ( prob ) return entropy
Calculate the number of entropy bits in a list or tuple of elements.