# importing mean()
from statistics import mean
def Average(Input):
Output =[] # Initialising a blank Output List
for x in Input:
a = round(mean(x),3) # Rounding the mean value to 3 decimal digits
t= (a,) # Making tuple with Mean
Output.append(t) # Making the list of Mean tuples
return Output
Input_1=[[2,4,6],[4,8,1]]
Input_2=[2,6],[8,6],[1,5],[4,5,1]
print(Average(Input_1))
print(Average(Input_2))
from statistics import mean
Input_1 = [[2,4,6], [4,8,1]]
Input_2 = [2,6], [8,6], [1,5], [4,5,1]
def mean_in_tuple(it):
return (mean(it), )
for it in [Input_1, Input_2]:
result = list(map(mean_in_tuple, it))
print(result)
下面是一个使用列表理解的简单解决方案
如果不想使用
statistics
库,可以做的另一件事是下面是一个完成此任务的简单方法
您可以使用高阶函数
map
(Python继承自LISP),它将函数应用于iterable(如列表)的所有成员:乍一看,这和列表理解没什么不同。但它可以导致一种完全不同的编码风格,称为函数式编程。这种风格通常更容易推理和测试。它还可以很好地组合成管道:
相关问题 更多 >
编程相关推荐