遍历并通过 for 循环将值添加到数组

Loop through and add values to array via for loop

早上好,我正在尝试将我所有的距离和速度值添加到一个数组中,以便我可以使用 PLT 绘制它们。但是,python 只是附加了 1 个值,为什么呢?我究竟做错了什么?代码和输出如下: '''

import cv2
import matplotlib.pyplot as plt
import numpy as np
list=["spect2.png","spect3.png","spect4.png","spect5.png","spect6.png", 
"spect7.png","spect8.png","spect9.png","spect11.png"]
for i in range(len(list)):
    curr_list=list[i]
    print("This is the image name reading: " + curr_list)

    im = cv2.imread(curr_list)

    red = [0,0,255]

    X,Y = np.where(np.all(im==red,axis=2))
    try:
        Average_X=sum(Y)/len(Y)
    except ZeroDivisionError:
        print("Zero div, cant")
    print(Average_X)


    yellow=[0,242,255]

    X,Y=np.where(np.all(im==yellow,axis=2))
    try:
        Average_Y=sum(Y)/len(Y)
    except ZeroDivisionError:
         print("Zero div, cant")
    print(Average_Y)

    amount_shifted=(Average_Y-Average_X)/Average_X

    speed=amount_shifted*3000000

    Distance=speed/22

    print("Speed away from us: "+str(speed))

    print("Distance away from us in mpc: "+str(Distance))
    curr_dis=Distance
    curr_amo=speed
    amount_list=[Distance]
    amount_list.append(curr_dis)
    amount_list_shifted=[speed]
    amount_list_shifted.append(curr_amo)


fig, ax = plt.subplots(figsize=(10, 6))
print(amount_list)
print(amount_list_shifted)
ax.scatter(amount_list, amount_list_shifted)
plt.show()

输出:

This is the image name reading: spect2.png
323.0
329.2345679012346
Speed away from us: 57906.20341703948
Distance away from us in mpc: 2632.1001553199767
This is the image name reading: spect3.png
361.0
361.0
Speed away from us: 0.0
Distance away from us in mpc: 0.0
This is the image name reading: spect4.png
304.0
Zero div, cant
361.0
Speed away from us: 562500.0
Distance away from us in mpc: 25568.18181818182
This is the image name reading: spect5.png
341.0
350.6666666666667
Speed away from us: 85043.98826979489
Distance away from us in mpc: 3865.6358304452224
This is the image name reading: spect6.png
407.0
411.0
Speed away from us: 29484.029484029485
Distance away from us in mpc: 1340.1831583649766
This is the image name reading: spect7.png
413.0
418.6441717791411
Speed away from us: 40998.82648286526
Distance away from us in mpc: 1863.583021948421
This is the image name reading: spect8.png
342.0
354.95454545454544
Speed away from us: 113636.36363636349
Distance away from us in mpc: 5165.28925619834
This is the image name reading: spect9.png
343.0
348.9032258064516
Speed away from us: 51631.71259287102
Distance away from us in mpc: 2346.896026948683
This is the image name reading: spect11.png
342.0
343.5409836065574
Speed away from us: 13517.400057521088
Distance away from us in mpc: 614.4272753418677
[614.4272753418677, 614.4272753418677]
[13517.400057521088, 13517.400057521088]

''' 如您所见,数组仅再次显示距离和相同的距离,我如何才能将所有距离和速度值相加?

在每次循环迭代中,您都非常明确地删除了之前的列表,并将其替换为当前数据的两个副本。我将只关注一个变量:

curr_dis=Distance                # the two variables are now the same value
amount_list=[Distance]           # over-write amount_list with only the current value
amount_list.append(curr_dis)     # append another copy of the current value

您的编程技能有两点需要提升:

(1) 学习基本调试。如果不出意外,插入策略性 print 语句以显示感兴趣的变量,并跟踪您的程序流程。

(2) 学习数据结构的基本技术。在这种情况下,您需要重复有关处理序列(字符串、列表、元组等)的教程材料。累积列表的基本模式是

a = []
while ... :
    create a new_value for the list
    a.append(new_value)

print(a)

你能从这里说完吗?