a = [1, 2 ,3]
b = [4, 5, 6]
c = [7, 8 ,9]
d = [[1, 2, 3, 4, 5],
[6, 7, 8, 9, 1],
[2, 3, 4, 5, 6],
[7, 8, 9, 1, 2]]
# 期望输出
# avg(a, b, c) = [(1+4+7)/3, (2+5+8)/3, (3+6+9)/3]
# avg(d) = [(1+6+2+7)/4, (2+7+3+8)/4, (3+8+4+9)/4, (4+9+5+1)/4, (5+1+6+2)/4]
avg代码
# 方法定义
def avg(*args):
result = []
for items in zip(*args):
result.append(sum(items) / len(items))
return result
# 调用方式
result = avg(a, b, c) # 多个列表a,b,c分别输入
result = avg(*d) # 多个列表合并为一个列表后输入