简体   繁体   English

如何使用 For 循环在 Python 中打印所有需要的值?

[英]How to Use a For Loop to Print All Desired Values in Python?

I have five files that I want to loop through so I can get all of the density dens values at the end.我有五个文件要循环查看,以便我可以在最后获得所有密度dens值。 The code is the same except where I have i.除了我有 i 之外,代码是相同的。 The issue I'm having is that when I do print(dens) outside of the loop I am only getting the values from the last file (the fifth one).我遇到的问题是,当我在循环之外执行print(dens)时,我只从最后一个文件(第五个文件)中获取值。 How do I fix the code to have all of the values from all of the files?如何修复代码以获取所有文件中的所有值? I'm thinking I need to change dens.append at the end of the code but I'm not sure how.我想我需要在代码末尾更改 dens.append 但我不确定如何更改。 Just for reference this code is to interpolation between latitudes/longitudes to get density values.仅供参考,此代码用于在纬度/经度之间进行插值以获得密度值。

for i in range(0,5):
    dens = [] # creating an empty list in order to make dens array from fifth day model output at the end of the for loop 
    for x, y in zip(satlong, satlat): # creating a loop to go through both satellite lat and long arrays 
    
        decindx_long = x/2 
    
        # model longitude parameter values at four corners of grid 
        v1_long = int(decindx_long)
        v3_long = int(decindx_long)
        v2_long = v1_long + 1
        v4_long = v1_long + 1
    
        # fraction for the longitude
        f = decindx_long - v1_long
    
        decindx_lat = (y - (-90))/2
    
        # model latitude parameter values at four corners of grid
        v1_lat = int(decindx_lat)
        v2_lat = int(decindx_lat)
        v3_lat = v1_lat + 1
        v4_lat = v1_lat + 1
    
        # fraction for the latitude 
        g = decindx_lat - v1_lat
    
        # parameter value between longitude grid values at latitude 1
        vp12 = density[i,v1_lat,v1_long] + f*((density[i, v2_lat, v2_long]) - (density[i, v1_lat, v1_long]))
    
        # parameter value between longitude grid values at latitude 2
        vp34 = density[i,v4_lat,v4_long] + f*((density[i,v4_lat,v4_long]) - (density[i,v3_lat,v3_long]))
    
        # value of parameter: the result of interpolation
        vp = vp12 + g*(vp34 - vp12)
        # creating density array for ephemeris data that was just interpolated
        dens.append(vp)

Create dens = [] above the for loop.在 for 循环上方创建dens = [] You create it every time you take a new file to process, therefore only the last one is taken into consideration.每次处理新文件时都会创建它,因此只考虑最后一个。

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

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