def average_precision(ranked_list, ground_truth):
    """Compute the average precision (AP) of a list of ranked items
    """
    hits = 0
    sum_precs = 0
    for index in range(len(ranked_list)):
        if ranked_list[index] in ground_truth:
            hits += 1
            sum_precs += hits / (index + 1.0)
    if hits > 0:
        return sum_precs / len(ground_truth)
    return 0