Python生成器对象打印错误排查:评分相关分母计算遇问题
Hey there! Let's break down why you're seeing that <generator object...> message instead of your expected calculation result, and fix it step by step.
What's Going Wrong?
- Generator Expression Behavior: You defined
denominatoras a generator expression (using parentheses()). Generators are lazy—they don't compute values until you iterate over them. Printing a generator directly just shows its object identity, not the computed results. - Logical Error in Calculation: Your current code runs
sum((a - average1) ** 2)for each individualain the zip pair. Since(a - average1) ** 2is a single value wrapped in parentheses, thesum()does nothing useful here—it just returns the same value. This is almost certainly not what you intended (you probably want to calculate the sum of squared deviations for the entire list, not per-element).
Fix 1: Get the Correct Global Denominator (Most Likely Your Goal)
If you're trying to compute a single denominator value (like for Pearson correlation, which uses the product of the square roots of sum of squared deviations for both lists), here's the corrected code:
First, make sure you import the math module (it's missing in your snippet):
import math average1 = 3.82 average2 = 3.98 short_rating1 = [3.5, 4.5, 4.0, 3.5, 4.0, 4.5, 3.5, 4.0, 2.0, 4.0, 3.0, 5.0, 4.5, 4.5, 3.0, 3.5, 4.0, 4.5, 2.0, 5.0, 3.0, 4.0, 3.0, 4.0, 5.0] short_rating2 = [3.5, 4.5, 5.0, 4.5, 5.0, 3.5, 4.0, 4.5, 3.5, 4.5, 3.0, 3.5, 3.0, 5.0, 4.0, 4.5, 4.0, 4.0, 3.0, 3.5, 4.0, 4.0, 4.0, 4.5, 3.0] # Calculate sum of squared deviations for entire lists first sum_sq_dev1 = sum((x - average1) ** 2 for x in short_rating1) sum_sq_dev2 = sum((y - average2) ** 2 for y in short_rating2) # Compute the final denominator denominator = math.sqrt(sum_sq_dev1) * math.sqrt(sum_sq_dev2) print(denominator)
This will output a single numerical value instead of a generator object.
Fix 2: If You Actually Need Per-Pair Values
If you did intend to compute a value for each (a, b) pair (though this is less common for denominator calculations), adjust the code to remove the unnecessary sum() calls and convert the generator to a list to see results:
import math average1 = 3.82 average2 = 3.98 short_rating1 = [3.5, 4.5, 4.0, 3.5, 4.0, 4.5, 3.5, 4.0, 2.0, 4.0, 3.0, 5.0, 4.5, 4.5, 3.0, 3.5, 4.0, 4.5, 2.0, 5.0, 3.0, 4.0, 3.0, 4.0, 5.0] short_rating2 = [3.5, 4.5, 5.0, 4.5, 5.0, 3.5, 4.0, 4.5, 3.5, 4.5, 3.0, 3.5, 3.0, 5.0, 4.0, 4.5, 4.0, 4.0, 3.0, 3.5, 4.0, 4.0, 4.0, 4.5, 3.0] # Create generator for per-pair values (removed redundant sum()) denominator_generator = (math.sqrt((a - average1) ** 2) * math.sqrt((b - average2) ** 2) for a, b in zip(short_rating1, short_rating2)) # Convert generator to list to see all computed values print(list(denominator_generator))
Key Takeaways
- Generators don't compute values until you iterate over them (using
list(), a for loop, etc.). - Always double-check where you're applying aggregation functions like
sum()—applying them to single elements won't give you the global statistics you need.
内容的提问来源于stack exchange,提问作者sayaton

