我无法导入任何模块来查找平均值,仅能找到“ for循环”。 实际上,我必须找到嵌套列表的平均值。
def get_average(map: List[List[int]]) -> float:
"""Return the average across all cells in the map.
>>> get_average(3X3)
5.0
>>> get_average(4X4)
3.8125
"""
total = 0
for sublist in range(len(map)): #gives sublist
for i in range(sublist): #access the items in the sublist
total = total + i
average = total / len(map)
return average
get_average(4X4)的输出为1.0
答案 0 :(得分:3)
L =[[1, 2, 6, 5],
[4, 5, 3, 2],
[7, 9, 8, 1],
[1, 2, 1, 4]]
def func(l):
total_sum = sum([sum(i) for i in l])
# make the sum of inner lists, store them in the list and then get the sum of final list
count = sum([len(i) for i in l]) # get the total element in the list
return total_sum/count # return average
print(func(L))
输出
3.8125
应该是什么操作代码
def get_average_elevation(elevation_map):
"""Return the average elevation across all cells in the elevation map
elevation_map.
Precondition: elevation_map is a valid elevation map.
>>> get_average_elevation(UNIQUE_3X3)
5.0
>>> get_average_elevation(FOUR_BY_FOUR)
3.8125
"""
total = 0
count = 0
for sublist in range(len(elevation_map)): # gives sublist index
for i in range(len(elevation_map[sublist])): # gives index of item in sublist
count+=1
total = total + elevation_map[sublist][i]
return total/count
l = [[1, 2, 6, 5],
[4, 5, 3, 2],
[7, 9, 8, 1],
[1, 2, 1, 4]]
print(get_average_elevation(l))
为什么要违抗,这是因为
让列表为l = [1,2,3,4]
因此,对于此列表for i in range(len(l))
仅迭代4次,但它不会给出列表内的元素(op认为它将给出),但是range返回一个在包含范围内进行迭代的对象,很容易将其赋予list从头到尾1。
为此,op要在列表中包含什么元素,他需要使用for element in list
这将给出单个元素,在此任务中,将其视为内部列表。
还要获取平均,op需要获取他正在选择的所有元素的和,但是他需要使avg超出for循环。
op还需要一个计数器来计算元素总数以求平均值。
答案 1 :(得分:2)
您误解了索引和列表内容之间的区别。
for sublist in range(len(elevation_map)): #gives sublist
不,不是。 sublist
遍历elevation_map
的索引,值为0-3。
for i in range(sublist): #access the items in the sublist
再次,不。 i
遍历0-sublist
的值,范围为0-3。
结果,total
的总和为0 + (0 + 1) + (0 + 1 + 2) = 4
。那就是你得到的平均值为1.0
相反,编写循环以按照注释描述的方式工作:
def get_average_elevation(elevation_map):
"""Return the average elevation across all cells in the elevation map
elevation_map.
Precondition: elevation_map is a valid elevation map.
>>> get_average_elevation(UNIQUE_3X3)
5.0
>>> get_average_elevation(FOUR_BY_FOUR)
3.8125
"""
total = 0
for sublist in elevation_map: #gives sublist
for i in sublist: #access the items in the sublist
total = total + i
average = total / len(elevation_map)
return average
现在,这将把所有16个元素相加并除以行的数量,结果是15.25
。我认为您需要元素的数量,因此您需要计算或计算这些元素。
你能从那里拿走吗?
答案 2 :(得分:1)
您的代码可能无法正常工作,因为for sublist in range(len(elevation_map)):
将在生成[1,2,3,4]的生成器上进行迭代。您永远不会访问elevation_map
数组中的实际数字。内循环也遇到同样的问题。
您可以使用列表推导来展平数组,然后从展平后的列表中获取平均值,从而简化代码。
flat_list = [item for sublist in elevation_map for item in sublist]
average = sum(flat_list) / len(flat_list)
答案 3 :(得分:0)
您只需将列表列表转为平面列表,然后使用sum
函数:
FOUR_BY_FOUR = [[1, 2, 6, 5],
[4, 5, 3, 2],
[7, 9, 8, 1],
[1, 2, 1, 4]]
UNIQUE_3X3 = [[1, 2, 3],
[9, 8, 7],
[4, 5, 6]]
answer = [i for k in FOUR_BY_FOUR for i in k]
print(sum(answer)/16)
answer = [i for k in UNIQUE_3X3 for i in k]
print(sum(answer)/9)
这将返回:
3.8125
5.0