“模块”对象不可下标是什么意思?

时间:2019-11-08 21:58:42

标签: python module

考虑到CSV的时间和距离(2000点),我需要计算速度和加速度:

import math

import numpy as np

import pandas as pd

#Excercise 2a

#input the csv

df = pd.read_csv('Data_oscillator.csv')

time = df['Time'].values

distance = df['x(t)'].values

velob = np.zeros(2000)

accelb = np.zeros(2000)

# differentiating distance to find new velocity and acceleration 

for i in range (0,1999):

    velob[i] = (distance[i+1] - distance[i])/(time[i+1] - time [i])

for i in range (0,1999):

    accelb[i] = (velob[i+1] - velob[i])/(time[i+1] - time[i])

我收到以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

<ipython-input-28-abd6ce780481> in <module>

      5 # differentiating distance to find new velocity and acceleration

      6 for i in range (0,1999):

----> 7     velob[i] = (distance[i+1] - distance[i])/(time[i+1] - time [i])

      8 

      9 for i in range (0,1999):


TypeError: 'module' object is not subscriptable

2 个答案:

答案 0 :(得分:0)

时间和[i]之间第二行的末尾似乎有一个空格,这也许就是您收到此错误的原因。

for i in range (0,1999):
    velob[i] = (distance[i+1] - distance[i])/(time[i+1] - time [i])

“时间”也是Python标准库中模块的名称。

答案 1 :(得分:0)

变量time的名称与内置的Python库冲突,该库可以作为模块导入(请参见here)。不允许将下标[i]应用于此,因此会出现错误消息。尝试将变量名称更改为time以外的名称。通常,最好避免使用可能导致与模块发生类似冲突的通用变量名称;而是使用更具描述性的变量名称:

import math

import numpy as np

import pandas as pd

#Excercise 2a

#input the csv

df = pd.read_csv('Data_oscillator.csv')

timeValues = df['Time'].values

distance = df['x(t)'].values

velob = np.zeros(2000)

accelb = np.zeros(2000)

# differentiating distance to find new velocity and acceleration 

for i in range (0,1999):

    velob[i] = (distance[i+1] - distance[i])/(timeValues[i+1] - timeValues[i])

for i in range (0,1999):

    accelb[i] = (velob[i+1] - velob[i])/(timeValues[i+1] - timeValues[i])