Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中将列表元组添加到列表中_Python_List_Tuples - Fatal编程技术网

在python中将列表元组添加到列表中

在python中将列表元组添加到列表中,python,list,tuples,Python,List,Tuples,我不理解将其元素也是列表元素的元组添加到包含所有信息的另一个列表的语法 我试图创建一个轨迹列表,其中包含飞行过程中弹丸飞行数据的元组。我想使用元组,这样我就可以看到每个时刻的所有信息 import random import math gg = -9.81 # gravity m/s**2 tt = 10 **-9 # seconds wind = random.randint(-10,10) # m/s #position x=random.randint(0,100) # m/s

我不理解将其元素也是列表元素的元组添加到包含所有信息的另一个列表的语法

我试图创建一个轨迹列表,其中包含飞行过程中弹丸飞行数据的元组。我想使用元组,这样我就可以看到每个时刻的所有信息

import random
import math

gg = -9.81 # gravity m/s**2
tt = 10 **-9 # seconds
wind = random.randint(-10,10)   # m/s

#position
x=random.randint(0,100)   # m/s

#projectile

v0 = float(raw_input('Enter the initial velocity (m/s) -> '));
theta = float(raw_input('Enter the initial launch angle of projectile (degrees) -> '));

theta *= (180/3.14159265359)

xx = [x]
yy = [.000000000000000000001]
dz =[v0]
time = [0];


data = ( time, xx, yy, dz)
traj = [data]


while yy[-1] >0:

    traj.append ( math.sqrt( (traj[-1][3][-1] * math.sin(theta) + gg * tt)** 2+        (traj[-1][4] * math.cos(theta) -wind) ** 2 ))    # velocity
    traj.append ( traj[-1][2][-1] + dz[-1] * math.sin(theta) * tt + .5* gg * tt)    # y position
    traj.append ( traj[-1][1][-1] * dz[-1] * math.cos(theta) - wind * tt)    # x position
    traj.append ( traj[-1][0][-1] + tt)     # time


print traj
编辑: 我将输入初始角度和速度的整数(即-45,45)。预期的输出将是一个元组列表,其中包含四个元素,分别对应于时间、x坐标、y坐标和速度。当前,我收到一个元组索引超出范围的错误。

您在哪里找到的

traj[-1][4]

在第一行
traj.append
中,
traj[-1]
data
,而
data
只有四个元素长,因此最后一项位于索引3。

代码的问题是,您将while循环中计算的值附加到
traj
列表中。这不会更新列表
xx
yy
时间
dz
。 您可以用以下方式修改代码

while yy[-1] > 0:
    dz_next = math.sqrt( (yy[-1] * math.sin(theta) + gg * tt)** 2+(dz[-1] * math.cos(theta) -wind)** 2)
    yy_next = yy[-1] + dz[-1] * math.sin(theta) * tt + .5* gg * tt
    xx_next = xx[-1] * dz[-1] * math.cos(theta) - wind * tt

    dz.append(dz_next)    # velocity
    yy.append(yy_next)    # y position
    xx.append(xx_next)    # x position
    time.append(time[-1] + tt)     # time
我认为更好的方法是

data = ( 0, x, 1e-20, v0) # initial data
traj = [data]

while True:
    t, x, y, v = traj[-1]
    if y < 0:
        break

    traj.append((t + tt, # time
                 x * v * math.cos(theta) - wind * tt, # x position
                 y + v * math.sin(theta) * tt + .5* gg * tt, # y position
                 (y* math.sin(theta) + gg * tt)** 2 + (v*math.cos(theta) -wind) ** 2) # velocity
    )


    print traj
    t_traj, x_traj, y_traj, z_traj = zip(*traj)
data=(0,x,1e-20,v0)#初始数据
traj=[数据]
尽管如此:
t、 x,y,v=traj[-1]
如果y<0:
打破
traj.append((t+tt,#time
x*v*math.cos(θ)-风*tt,#x位置
y+v*math.sin(θ)*tt+0.5*gg*tt,#y位置
(y*math.sin(θ)+gg*tt)**2+(v*math.cos(θ)-风)**2)#速度
)
打印traj
t_traj,x_traj,y_traj,z_traj=zip(*traj)

如果您显示:1,将会很有帮助。样本输入2。预期产出3。实际输出欢迎来到S.O。!您应该告诉我们您的输入和输出是什么,以及您的输出与您的期望有何不同。您的while循环中存在多个问题,您需要在循环中打印出traj,以查看哪里出错除了这里的其他问题,
theta*=(180/3.14159265359)
是错误的,因为它将弧度转换为度;你想要
theta*=3.14159265359/180
,或者更好,
theta=math.radians(theta)
。我诊断了我看到的第一个错误@Padraic。请随时为他/她重写整个程序,并纠正一切。错误太多了,我必须重写90%的代码,OP将通过自己的调试了解更多信息。我有可能是在挖苦人。既然你有精力挖苦人,既然你已经开始回答这个问题,你不妨自己重写所有代码;)