简体   繁体   English

获取Python中坐标列表的平均值

[英]Getting the average of a list of coordinates in python

I have a list of coordinates like [[1, 2, 2], [1, 2, 1], [1, 1, 1]] in python, and I want to get the average of them, such as in this case is [1, 1.66666666, 1.333333333] . 我在python中有一个坐标列表,例如[[1, 2, 2], [1, 2, 1], [1, 1, 1]] 1、1、1 [[1, 2, 2], [1, 2, 1], [1, 1, 1]] ,我想获取它们的平均值,例如在这种情况下是[1, 1.66666666, 1.333333333] However, I can't quite figure out how to do that. 但是,我不太清楚该怎么做。 I have tried NumPy and basic list manipulation but they all failed to work. 我尝试了NumPy和基本列表操作,但它们均无法正常工作。 Can someone help me? 有人能帮我吗? I am using python 3.6 . 我正在使用python 3.6

I guess this is what you are looking for: 我想这就是您要寻找的东西:

data = [[1, 2, 2], [1, 2, 1], [1, 1, 1]]
average = [sum(x)/len(x) for x in zip(*data)]
print(average)

hope you're working with python 3.x. 希望您正在使用python3.x。

You can use NumPy arrays for a column wise average as following. 您可以按以下方式将NumPy数组用于按列的平均值。 axis=0 computes the average columnwise. axis=0计算列的平均值。 You can also use np.mean() here 您也可以在这里使用np.mean()

data =  np.array([[1, 2, 2], [1, 2, 1], [1, 1, 1]] )

averaged = np.average(data, axis=0)
print (averaged)
# [1.         1.66666667 1.33333333]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM