def ap_at_k(preds, targets, k=10):
    """
    Calculates the AP@K(Average Precision at K).

    Parameters
    -------------
    preds : list
        Predictions.
    targets : list
        Target (true) values.
    k : int, optional
        K in the MAP@K.

    Returns
    -------
    float
        AP@K.
    """
    hits = 0.
    acc = 0.
    for i in range(k):

        if preds[i] in targets:
            hits += 1.0
            acc += hits / (i + 1)

    return acc / min(k, len(targets))