当我的变量是整个列表而不是整数时使用for循环(python)

时间:2019-02-06 03:45:14

标签: python list loops for-loop

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

此代码的作用:我从一个文本文件(每个都有“ Npoints”元素)中读取了这些列表x1和vx1。之后,我对每个列表中的每个元素进行操作,最后剩下 new 列表x2,vx2

我要执行的操作:我想创建某种循环,在其中执行与在原始列表x1和vx2上执行的操作相同的操作,但是在我的新列表x2和vx2上执行相同的操作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 

2 个答案:

答案 0 :(得分:0)

您的代码几乎可以修改为循环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

答案 1 :(得分:0)

在这段代码中,我最终得到一个“列表列表”,它将为您进行的第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]