【问题标题】:Loop through and add values to array via for loop循环并通过for循环将值添加到数组
【发布时间】:2025-12-08 19:30:05
【问题描述】:

早上,我正在尝试将我的所有距离和速度值添加到一个数组中,以便我可以使用 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]

''' 如您所见,该数组仅再次显示距离和相同的距离,我如何让它添加所有距离和速度值?

【问题讨论】:

    标签: python arrays numpy matplotlib astronomy


    【解决方案1】:

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

    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)
    

    你能从这里结束吗?

    【讨论】:

    • 谢谢,快速提问,while 循环的条件是什么?我的意思是,我将如何积累它?条件是 for 循环本身吗?
    • 再次参考教程。这适用于任何循环;你已经有一个for。当您对现有代码进行这种累积更改时出了什么问题?
    • 就像,我不明白逻辑。就像在While(条件)中一样,满足while循环的条件是什么
    • 你是决定循环做什么的人——你必须根据你的应用程序对该条件进行编码。正如我之前所说,将其应用于您的 for 循环。