Esempio n. 1
0
def test_colfunc():
    data = ['a3', 'b2', 'c1']
    assert list(map(colfunc(None), data)) == data
    assert list(map(colfunc(0), data)) == ['a', 'b', 'c']
    assert list(map(colfunc(1), data)) == ['3', '2', '1']
    expected = [['3', 'a'], ['2', 'b'], ['1', 'c']]
    assert list(map(colfunc((1, 0)), data)) == expected
Esempio n. 2
0
def Mean(iterable, key=None, default=None):
    """
    iterable >> Mean(key=None, default=None)

    Return mean value of inputs (transformed or extracted by key function).

    >>> [1, 2, 3] >> Mean()
    2.0

    >>> [] >> Mean(default=0)
    0

    >>> data = [(1, 10), (2, 20), (3, 30)]
    >>> data >> Mean(key=0)
    2.0
    >>> data >> Mean(key=1)
    20.0

    :param iterable iterable: Iterable over numbers
    :param object default: Value returned if iterable is empty.
    :param int|tuple|function|None key: Key function to extract elements.
    :return: Mean of numbers or default value
    :rtype: number
    """
    f = colfunc(key)
    sum, n = 0, 0
    for e in iterable:
        sum += f(e)
        n += 1
    return float(sum) / n if n else default
Esempio n. 3
0
def Sort(iterable, key=None, reverse=False):
    """
    iterable >> Sort(key=None, reverse=False)

    Sorts iterable with respect to key function or column index(es).

    >>> [3, 1, 2] >> Sort()
    [1, 2, 3]

    >>> [3, 1, 2] >> Sort(reverse=True)
    [3, 2, 1]

    >>> [(1,'c'), (2,'b'), (3,'a')] >> Sort(1)
    [(3, 'a'), (2, 'b'), (1, 'c')]

    >>> ['a3', 'c1', 'b2'] >> Sort(key=lambda s: s[0])
    ['a3', 'b2', 'c1']

    >>> ['a3', 'c1', 'b2'] >> Sort(key=0)
    ['a3', 'b2', 'c1']

    >>> ['a3', 'c1', 'b2'] >> Sort(1)
    ['c1', 'b2', 'a3']

    >>> ['a3', 'c1', 'b2'] >> Sort((1,0))
    ['c1', 'b2', 'a3']

    :param iterable iterable: Iterable
    :param int|tuple|function|None key: function to sort based on or
           column index(es) tuples/vectors/strings are sorted by.
    :param boolean reverse: True: reverse order.
    :return: Sorted iterable
    :rtype: list
    """
    return sorted(iterable, key=colfunc(key), reverse=reverse)
Esempio n. 4
0
def Min(iterable, key=None, default=None):
    """
    iterable >> Min(key=None, default=None)

    Return minimum of inputs (transformed or extracted by key function).

    >>> [1, 2, 3, 2] >> Min()
    1

    >>> ['1', '123', '12'] >> Min(key=len)
    '1'

    >>> [] >> Min(default=0)
    0

    >>> data = [(3, 10), (2, 20), (1, 30)]
    >>> data >> Min(key=0)
    (1, 30)

    >>> data >> Min(1)
    (3, 10)


    :param iterable iterable: Iterable over numbers
    :param int|tuple|function|None key: Key function to extract or
           transform elements. None = identity function.
    :param object default: Value returned if iterable is empty.
    :return: smallest element according to key function
    :rtype: object
    """
    try:
        return min(iterable, key=colfunc(key))
    except Exception:
        return default
Esempio n. 5
0
def ArgMin(iterable, key=None, default=None, retvalue=False):
    """
    iterable >> ArgMin(key=None, default=None, retvalue=True)

    Return index of first minimum element (and minimum) in input
    (transformed or extracted by key function).

    >>> [1, 2, 0, 2] >> ArgMin()
    2

    >>> ['12', '1', '123'] >> ArgMin(key=len, retvalue=True)
    (1, '1')

    >>> ['12', '1', '123'] >> ArgMin(key=len)
    1

    >>> [] >> ArgMin(default=0)
    0

    >>> [] >> ArgMin(default=(None, 0), retvalue=True)
    (None, 0)

    >>> data = [(3, 10), (2, 20), (1, 30)]
    >>> data >> ArgMin(key=0)
    2
    >>> data >> ArgMin(1)
    0


    :param iterable iterable: Iterable over numbers
    :param int|tuple|function|None key: Key function to extract or
           transform elements. None = identity function.
    :param object default: Value returned if iterable is empty.
    :param bool retvalue: If True the index and the value of the
           minimum element is returned.
    :return: index of smallest element according to key function
             and the smallest element itself if retvalue==True.
    :rtype: object | tuple
    """
    try:
        f = colfunc(key)
        i, v = min(enumerate(iterable), key=lambda i_e1: f(i_e1[1]))
        return (i, v) if retvalue else i
    except Exception:
        return default
Esempio n. 6
0
def Sum(iterable, key=None):
    """
    iterable >> Sum(key=None)

    Return sum over inputs (transformed or extracted by key function)

    >>> [1, 2, 3] >> Sum()
    6

    >>> [1, 2, 3] >> Sum(lambda x: x*x)
    14

    >>> data = [(1, 10), (2, 20), (3, 30)]
    >>> data >> Sum(key=0)
    6
    >>> data >> Sum(key=1)
    60

    :param iterable iterable: Iterable over numbers
    :param int|tuple|function|None key: Key function to extract elements.
    :return: Sum of numbers
    :rtype: number
    """
    return sum(map(colfunc(key), iterable))
Esempio n. 7
0
def MeanStd(iterable, key=None, default=None, ddof=1):
    """
    iterable >> MeanStd(key=None, default=None, ddof=1)

    Return mean and standard deviation of inputs (transformed or extracted
    by key function).
    Standard deviation is with degrees of freedom = 1

    >>> [1, 2, 3] >> MeanStd()
    (2.0, 1.0)

    >>> data = [(1, 10), (2, 20), (3, 30)]
    >>> data >> MeanStd(key=0)
    (2.0, 1.0)
    >>> data >> MeanStd(1)
    (20.0, 10.0)

    :param iterable iterable: Iterable over numbers
    :param object default: Value returned if iterable is empty.
    :param int|tuple|function|None key: Key function to extract elements.
    :param int ddof: Delta degrees of freedom (should 0 or 1)
    :return: Mean and standard deviation of numbers or default value
    :rtype: tuple (mean, std)
    """
    f = colfunc(key)
    sume, sqre, n = 0.0, 0.0, 0.0
    for e in iterable:
        e = f(e)
        sume += e
        sqre += e * e
        n += 1.0
    if n - ddof <= 0:
        return default
    avg = sume / n
    dev = math.sqrt((n * sqre - sume * sume) / (n * (n - ddof)))
    return avg, dev