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.
Python code
33 linesdef 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
{'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
- Use collections.Counter with a generator expression to build counts in one line
- 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
More from Database scaling & optimization
- Approximate Count with HyperLogLog in Python medium
- B-Tree Insert and In-Order Traversal in Python hard
- Broadcast a Small Reference Table in Python easy
- Build a Full Text Search Index in Python medium
- Build a Partial Index Mock in Python for Database Filtering easy
- Composite index leftmost prefix in Python medium
Keep learning
Related tutorials and quizzes for this topic.