Practical Examples
Real-World List Comparison Scenarios
Data Validation and Filtering
def validate_student_scores(expected_scores, actual_scores):
"""
Compare student scores with expected benchmark
"""
passing_threshold = 60
valid_scores = [
score for score in actual_scores
if score in expected_scores and score >= passing_threshold
]
return {
'valid_count': len(valid_scores),
'valid_scores': valid_scores
}
## Example usage
expected = [65, 70, 75, 80, 85]
actual = [55, 65, 70, 72, 90, 45]
result = validate_student_scores(expected, actual)
print(result)
Inventory Management Comparison
def compare_inventory(warehouse1, warehouse2):
"""
Compare inventory between two warehouses
"""
shared_items = set(warehouse1) & set(warehouse2)
unique_to_warehouse1 = set(warehouse1) - set(warehouse2)
unique_to_warehouse2 = set(warehouse2) - set(warehouse1)
return {
'shared_items': list(shared_items),
'unique_to_warehouse1': list(unique_to_warehouse1),
'unique_to_warehouse2': list(unique_to_warehouse2)
}
## Example
warehouse1 = ['apple', 'banana', 'orange', 'grape']
warehouse2 = ['banana', 'orange', 'mango', 'kiwi']
inventory_comparison = compare_inventory(warehouse1, warehouse2)
print(inventory_comparison)
Comparative Analysis Techniques
def compare_performance_metrics(baseline, current):
"""
Compare performance metrics with percentage change
"""
comparison_results = []
for baseline_value, current_value in zip(baseline, current):
change_percentage = ((current_value - baseline_value) / baseline_value) * 100
comparison_results.append({
'baseline': baseline_value,
'current': current_value,
'change_percentage': round(change_percentage, 2)
})
return comparison_results
## Example
baseline_metrics = [100, 200, 300]
current_metrics = [110, 180, 350]
performance_comparison = compare_performance_metrics(baseline_metrics, current_metrics)
print(performance_comparison)
Advanced Comparison Strategies
Complex List Comparison
graph LR
A[Input Lists] --> B{Comparison Method}
B --> |Intersection| C[Common Elements]
B --> |Difference| D[Unique Elements]
B --> |Symmetric Difference| E[Non-Overlapping Elements]
Multi-Dimensional List Comparison
def multi_dimensional_comparison(lists):
"""
Compare multiple lists across different dimensions
"""
comparison_matrix = []
for i in range(len(lists)):
row = []
for j in range(len(lists)):
similarity = len(set(lists[i]) & set(lists[j])) / len(set(lists[i]) | set(lists[j]))
row.append(round(similarity, 2))
comparison_matrix.append(row)
return comparison_matrix
## Example
data_lists = [
[1, 2, 3, 4],
[3, 4, 5, 6],
[2, 4, 6, 8]
]
result = multi_dimensional_comparison(data_lists)
print(result)
Comparison Complexity Matrix
Scenario |
Complexity |
Recommended Method |
Small Lists |
O(n) |
Direct Comparison |
Large Lists |
O(n log n) |
Sorted Comparison |
Unique Elements |
O(n) |
Set Conversion |
Performance Critical |
Varies |
Optimized Algorithms |
At LabEx, we believe mastering these practical list comparison techniques empowers developers to write more sophisticated and efficient Python code.