You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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?

  1. Generator Expression Behavior: You defined denominator as 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.
  2. Logical Error in Calculation: Your current code runs sum((a - average1) ** 2) for each individual a in the zip pair. Since (a - average1) ** 2 is a single value wrapped in parentheses, the sum() 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:52:17