当我的变量是一个完整的列表而不仅仅是一个交互器(python)时,使用for循环



我在这里附上了我的代码摘录,以帮助我说明我的问题。

这段代码的作用:我从文本文件中读取了这些列表 x1 和 vx1(每个都有"Npoints"元素(。之后,我对每个列表中的每个元素进行操作,最后我留下了新列表 x2,vx2

我想

这样做:我想创建某种循环,在其中执行与原始列表 x1 和 vx2 相同的操作,但在新列表 x2 和 vx2 上执行。这应该创建列表 x3 和 vx3,我可以再次操作......依此类推,直到我得到列表 xn、vxn。

另请注意,我已经有两个 for 循环继续对原始内容进行操作(我不知道这是否会使事情复杂化(。

希望你们能帮助我!我对Python很陌生,所以我非常感谢你能提供的任何建议。谢谢。:)

npoints=999
n1= []
mass1 = []
x1= []
vx1= []
fx_list=[]
G=1
dt=.0001
with open('myfile.dat') as f:
     for row in f.readlines():  
        if not row.startswith("#"):
            spaces=row.split('   ')
            n1.append(float(spaces[0]))
            mass1.append(float(spaces[1]))
            x1.append(float(spaces[2]))
            y1.append(float(spaces[3]))
            z1.append(float(spaces[4]))
            vx1.append(float(spaces[5]))
            vy1.append(float(spaces[6]))
            vz1.append(float(spaces[7]))
     for xn in range(0,npoints): 
          for step in range(0,npoints):
               #This is where I first operate on x1,
               fx=((G*mass1[xn]*mass1[step+1]*((x1[step+1]**2.)-(x1[xn]**2.)))/(abs((x1[step+1]**2)-(x1[xn]**2))**2.)**(3./2.))
               #Then put store it in an array
               fx_list.append(fx)
               fxx= np.array_split(fx_list,npoints)
               fxxx_list=[]
               for xn in range(0,npoints):
                    fxxx= np.sum(fxx[xn])
               #and save that in array. Now I have the accelearation on each particle. 
               fxxx_list.append(fxxx)
               #This is where i begin the integration
               #In other words, this is where I redefine the x/vx values
               vx2=[]
               for xn in range(0,npoints):
                    vx11=vx1[xn]+.5*(fxxx_list[xn]+fxxx_list[xn])*dt
                    vx2.append(vx11)
               x2=[]
               for xn in range(0,npoints):
                    x11=(x1[xn]+vx2[xn]*dt)+(.5*fxxx_list[xn]*(dt**2))
                    x2.append(x11)
               print x2 vx2 #and I want to put these value into where x1 is and loop the whole thing again N number of times 

您的代码可以非常少地修改为循环 N 次并在 x1vx1 上执行相同的功能。 您所需要的只是将嵌套的 for 循环包装在另一个循环中,并在每次迭代结束时重写x1vx1

我描述的基本思想相当常见,下面显示了一个通用实现。 在这种情况下,您的someFunction将是您为创建x2vx2而执行的整个嵌套 for 循环例程。

x1, vx1 = [], [] # x1 and vx1 are initialized to some values, as you do at the top of your with... as... block
# Loop N times, essentially performing the function on xn, vxn until you arrive at xN, vxN
for n in range(N):
    # Perform some function on x1 and vx1 that results in x2 and vx2
    # This function can be as complex as you need it to be, but if it is complex (as it seems to be), I would recommend moving it to its own function as I show here
    x2, vx2 = someFunction(x1, vx1)
    # Write over x1 and vx1 so they can be used in future iterations of the function
    x1, vx1 = x2, vx2
# x1 and vx1 are not essentially xN and vxN

在此代码中,我最终得到一个"列表列表",它将为您进行的每次第 n 次迭代创建数据历史记录。您执行超长外观功能,但为了演示,我只为每个元素添加 1。

x1 = [25, 40, 60, 100, 32, 51]
position_array = []
n_times = 5
position_array.append(x1[:])
for i in range(n_times):
    for j in range(len(x1)):
        x1[j] = x1[j] + 1
    position_array.append(x1[:])
print(position_array)

表示法position_array.append(x1[:])将第 n 个修改的 x1 列表的副本附加到新的"列表列表"中。规避一个频繁的初学者 python 编码人员错误,即一遍又一遍地附加相同的结果列表。所以这输出:

[[25, 40, 60, 100, 32, 51], 
[26, 41, 61, 101, 33, 52], 
[27, 42, 62, 102, 34, 53], 
[28, 43, 63, 103, 35, 54], 
[29, 44, 64, 104, 36, 55], 
[30, 45, 65, 105, 37, 56]]

如果您不想要第 n 次修改的历史记录,您可以这样做:

x1 = [25, 40, 60, 100, 32, 51]
position_array = []
n_times = 5
for i in range(n_times):
    for j in range(len(x1)):
        x1[j] = x1[j] + 1
print(x1)

这将简单地输出:

[30, 45, 65, 105, 37, 56]

相关内容

最新更新