【问题标题】:TypeError: can only concatenate list (not "int") to list 4TypeError:只能将列表(不是“int”)连接到列表 4
【发布时间】:2015-12-15 18:57:22
【问题描述】:

我需要为我的课程学习 Python 模块,但我的脚本出现此错误。它正在绘制弹丸的轨迹并计算其他一些变量。我已经完全按照我们提供的小册子输入了脚本。 因为我是一个绝对的初学者,所以我无法理解这个错误的其他答案。如果有人能给我一个快速修复,我将不胜感激,我目前没有时间学习足够的知识来自己修复它。

代码:

import matplotlib.pyplot as plt
import numpy as np
import math # need math module for trigonometric functions

g = 9.81 #gravitational constant
dt = 1e-3 #integration time step (delta t)
v0 = 40 # initial speed at t = 0

angle = math.pi/4 #math.pi = 3.14, launch angle in radians

time = np.arange(0,10,dt) #time axis
vx0 = math.cos(angle)*v0 # starting velocity along x axis
vy0 = math.sin(angle)*v0 # starting velocity along y axis

xa = vx0*time # compute x coordinates
ya = -0.5*g*time**2 + vy0*time # compute y coordinates

fig1 = plt.figure()
plt.plot(xa, ya) # plot y versus x
plt.xlabel ("x")
plt.ylabel ("y")
plt.ylim(0, 50)
plt.show()

    def traj(angle, v0): # function for trajectory
        vx0 = math.cos(angle) * v0 # for some launch angle and starting  velocity
    vy0 = math.sin(angle) * v0 # compute x and y component of starting velocity

    x = np.zeros(len(time))   #initialise x and y arrays
    y = np.zeros(len(time))

    x[0], y[0], 0 #projecitle starts at 0,0
    x[1], y[1] = x[0] + vx0 * dt, y[0] + vy0 * dt # second elements of x and
                                              # y are determined by initial 
                                              # velocity
    i = 1
    while y[i] >= 0: # conditional loop continuous until
    # projectile hits ground
        x[i+1] = (2 * x[i] - x[i - 1]) # numerical integration to find x[i + 1]                                       
        y[i+1] = (2 * y[i] - y[i - 1]) - g * dt ** 2 # and y[i + 1]

        i = [i + 1] # increment i for next loop


        x = x[0:i+1] # truncate x and y arrays                                                
        y = y[0:i+1]
        return x, y, (dt*i), x[i] # return x, y, flight time, range of projectile

x, y, duration, distance = traj(angle, v0)
print "Distance:" ,distance
print "Duration:" ,duration

n = 5
angles = np.linspace(0, math.pi/2, n)
maxrange = np.zeros(n)

for i in range(n):
    x,y, duration, maxrange [i] = traj(angles[i], v0)

angles = angles/2/math.pi*360 #convert rad to degress

print "Optimum angle:", angles[np.where(maxrange==np.max(maxrange))]

明确的错误:

  File "C:/Users/***** at *****", line 52, in traj
    x = x[0:i+1] # truncate x and y arrays

TypeError: can only concatenate list (not "int") to list

【问题讨论】:

  • i = [i + 1] 看起来很“可疑”,你能去掉方括号吗?
  • 修正你的身份。 i = [i + 1] 您正在将一个列表分配给一个 int。 x = x[0:i+1] 这看起来像是在更改列表(而不是截断),我敢打赌这不是 Python 或者它是您查找错误的测试?

标签: python list int concatenation typeerror


【解决方案1】:

正如 cmets 中指出的,这是违规行

i = [i + 1] # increment i for next loop

在这里,i 实际上并没有像评论所暗示的那样被递增。当i 为1 时,它被设置为[1 + 1],其计算结果为[2],该列表仅包含数字2。删除括号。

【讨论】:

  • 是的,这有效,谢谢大家的回答和解释。我不能投票给你 atm 但我相信其他人会:)
  • @tglaria 这是这个问题的主题
猜你喜欢
  • 1970-01-01
  • 2019-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-04
  • 1970-01-01
相关资源
最近更新 更多