Tipo · code-quality
def calculate_avg_usage(records):
total_usage = 0
user_count = 0
for record in records:
if 'user_id' in record and 'data_mb' in record:
total_usage += record['data_mb']
user_count += 1
if user_count > 0:
return total_usage / user_count
else:
return 0En inglés:Refactor the following Python code snippet, which calculates the average data usage per user from a list of usage records. Focus on improving readability, efficiency, and handling potential edge cases like empty input or missing data points.def calculate_avg_usage(records):
total_usage = 0
user_count = 0
for record in records:
if 'user_id' in record and 'data_mb' in record:
total_usage += record['data_mb']
user_count += 1
if user_count > 0:
return total_usage / user_count
else:
return 0