0
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((HOST,PORT))

    screen_width = 0
    screen_height = 0

    while True:
        client_socket.send("loc\n")
        data = client_socket.recv(8192)
        pos_coordinates = data.split()

        if(not(pos_coordinates[-1] == "eom" and pos_coordinates[0] == "start")):
            continue

        if (screenw != int(pos_coordinates[2])):
            screenw = int(pos_coordinates[2])
            screenh = int(pos_coordinates[3])

        blalba(pos_coordinates)

如何获得 3 个 pos_coordinates 列表值的平均值?例如,如果 pos_coordinates[8] 包含值 345,我想获得该值的平均值并使用平均值。我想这样做的原因是因为我从通过相机检测到的图像中得到了噪音,我认为最好的方法是获取这些值的平均值以获得我想要的值。有关如何执行此操作的任何示例?

4

1 回答 1

0

获取整个列表的平均值;

average = float(sum(pos_coordinates))/len(pos_coordinates)

如果您只想获得子集的平均值,请使用切片;

subset = pos_coordinates[3:9]
average = float(sum(subset))/len(subset)

在 python 3.x 中,float不再需要显式转换来获得准确的除法。

于 2012-09-30T06:29:13.693 回答