Python 只有整数、切片...和整数或布尔数组是有效的索引

Posted

技术标签:

【中文标题】Python 只有整数、切片...和整数或布尔数组是有效的索引【英文标题】:Python only integers, slices ... and integer or boolean arrays are valid indices 【发布时间】:2018-12-04 14:09:51 【问题描述】:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from scipy import integrate
from scipy.optimize import fmin

Td=np.array([0.5,1,1.5,2,2.2,3,3.5,4,4.5,5])#time
findindex=lambda x:np.where(mt>=x)[0][0]
mindex=map(findindex,Td)

Zm=Td[mindex]

当我尝试运行此代码时,它会引发异常。

发生异常:IndexError 只有整数、切片 (:)、省略号 (...)、numpy.newaxis (None) 和整数或布尔数组是有效的索引。

我是 Python 新手。 有人可以帮忙解决这个问题吗?

【问题讨论】:

你想达到什么目的?mt是什么? 【参考方案1】:

IIUC 您要过滤的索引满足某些条件,然后使用这些索引对 numpy 数组进行切片

mt=3
Td[np.where(Td<mt)] # where values in Td array less than mt get indexes and slice the array

【讨论】:

以上是关于Python 只有整数、切片...和整数或布尔数组是有效的索引的主要内容,如果未能解决你的问题,请参考以下文章

RandomForest IndexError:只有整数、切片(`:`)、省略号(`...`)、numpy.newaxis(`None`)和整数或布尔数组是有效的索引

只有整数、切片 (`:`)、省略号 (`...`)、numpy.newaxis (`None`) 和整数或布尔数组是生成 rnn 的有效索引

Python错误IndexError:只有整数

numpy——高级索引

在 Python 中使用字符串作为切片索引? (TypeError:切片索引必须是整数或 None 或具有 __index__ 方法)

TypeError:列表索引必须是整数或切片,而不是 str