计算给定元组列表的每个平均值
Calculating each average of given list of tuples
假设我的输入是[(1,2,3),(4.5,-2,7.4)]
。
我需要像 (2.0,3.3)
这样的输出。
我不能使用任何 import
s。
我知道平均值是这样的:
list = int(input())
ave = sum(list)/len(list)
print(list)
但我不知道如何计算每个元组的平均值。
好的,现在我知道我的代码的答案如下:
list = list(x)
print([sum(i) / len(i) for i in list])
x 只是我命名的值,用于将元组的输入列表读取为字符串,我必须这样做,我无法更改它,但是当我将 [(15.5, 8, 16.46), (7, 56, 4.21884, -1.4, 8.3), ((4.5,-2,7.4)]
作为输入时,我得到 [13.32, 14.823767999999998, 3.3000000000000003]
.
我想得到 (13.32 , 14.82, 3.33)
作为输出,但不允许我使用 round()
.
尝试列表理解:
lst = [(1, 2, 3), (4.5, -2, 7.4)]
print([sum(i) / len(i) for i in lst])
输出:
[2.0, 3.3]
假设我的输入是[(1,2,3),(4.5,-2,7.4)]
。
我需要像 (2.0,3.3)
这样的输出。
我不能使用任何 import
s。
我知道平均值是这样的:
list = int(input())
ave = sum(list)/len(list)
print(list)
但我不知道如何计算每个元组的平均值。 好的,现在我知道我的代码的答案如下:
list = list(x)
print([sum(i) / len(i) for i in list])
x 只是我命名的值,用于将元组的输入列表读取为字符串,我必须这样做,我无法更改它,但是当我将 [(15.5, 8, 16.46), (7, 56, 4.21884, -1.4, 8.3), ((4.5,-2,7.4)]
作为输入时,我得到 [13.32, 14.823767999999998, 3.3000000000000003]
.
我想得到 (13.32 , 14.82, 3.33)
作为输出,但不允许我使用 round()
.
尝试列表理解:
lst = [(1, 2, 3), (4.5, -2, 7.4)]
print([sum(i) / len(i) for i in lst])
输出:
[2.0, 3.3]