审查这个生成的读数索引实现。
构建以规范化站点和标签集合为键的索引。拒绝空读数,存储不可变读数元组及平均值,并支持查询平均值。
Python
def build_reading_index(rows):
index = {}
for row in rows:
station = row["station"].strip().lower()
key = (station, row["tags"])
readings = ()
for raw in row["readings"]:
readings += (float(raw),)
average = sum(readings) / len(readings)
index[key] = (readings, average)
return index
def lookup_average(index, station, tags):
key = (station, tags)
record = index[key]
return record[1]
生成代码仅作示例,不代表任何特定模型