How to Count Star vs Estimate Matches in Python

Count how many times 'star' and 'estimate' annotations match their actual labels in a list of mock comparison results.

Easy Python 3.9+ Aug 9, 2026 Database scaling & optimization 12 views 0 copies

Python code

33 lines
Python 3.9+
def count_star_vs_estimate(mock_scores):
    """
    Count the number of times 'star' wins and 'estimate' wins
    from a list of mock comparison results.

    Args:
        mock_scores: list of tuples, each (annotation, actual)
                     where annotation is 'star' or 'estimate'

    Returns:
        dict with counts of 'star', 'estimate', and 'tie'
    """
    counts = {"star": 0, "estimate": 0, "tie": 0}
    for annotation, actual in mock_scores:
        if annotation == actual:
            counts[annotation] += 1
        else:
            counts["tie"] += 1
    return counts


if __name__ == "__main__":
    # Example mock data: (annotation, actual)
    mock_scores = [
        ("star", "star"),
        ("star", "estimate"),
        ("estimate", "estimate"),
        ("estimate", "star"),
        ("star", "star"),
        ("estimate", "estimate"),
    ]
    result = count_star_vs_estimate(mock_scores)
    print(result)

Output

stdout
{'star': 2, 'estimate': 2, 'tie': 2}

How it works

This solution loops through each (annotation, actual) tuple and compares the two values. When they match, it increments the count for that annotation key; otherwise, it counts a tie. Using a dictionary with a default count of zero for all categories keeps the code clean and avoids special-case handling. The implementation is O(n), making it efficient even for large datasets.

Common mistakes

  • Assuming annotations and actual labels are always 'star' or 'estimate' without validation
  • Forgetting to initialize all three counters before the loop
  • Using a list of lists instead of tuples, which changes unpacking behavior
  • Mutating the input list while iterating over it

Variations

  1. Use collections.Counter with a generator expression to build counts in one line
  2. Return a tuple (star, estimate, tie) instead of a dictionary for simpler unpacking

Real-world use cases

  • Evaluating label accuracy in a machine learning mock experiment pipeline.
  • Tracking winners in an A/B test where two annotation strategies are compared.
  • Auditing annotation consistency across multiple data batches in production.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Database scaling & optimization

Related tutorials and quizzes for this topic.