You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

机器学习图像训练生成图的最大连续增长段检测及代码报错排查

问题排查与解决方案

错误根源定位

TypeError: list indices must be integers or slices, not str 主要由两个问题引发:

  1. DataFrame创建逻辑问题:totaldata[tempvideocount]中,若totaldata是普通Python列表,而tempvideocount是字符串类型,就会触发该错误——列表仅支持整数/切片索引,不支持字符串索引。
  2. 代码拼写错误:代码中误将列名Embryo_Size拼写成Embroyo_Size,导致访问DataFrame列时出错。

分步修复方案

1. 修正DataFrame创建问题

先确认totaldata的类型与tempvideocount的取值:

  • 若totaldata是字典:确保tempvideocount是字典中存在的键(整数/字符串类型需匹配)。
  • 若totaldata是列表:tempvideocount必须是整数索引,若当前是字符串,需先转为整数,比如tempvideocount = int(tempvideocount)。

可通过以下代码排查:

print(type(totaldata))
print(tempvideocount, type(tempvideocount))

2. 修正代码拼写错误

将代码中所有Embroyo_Size改为正确的Embryo_Size,修正后的核心逻辑代码:

# 划分连续增长分段:每次出现下降时,分段编号累加
df['is_increasing'] = df['Embryo_Size'].diff().lt(0).cumsum()
# 计算每个分段的长度
sizes = df.groupby('is_increasing')['Embryo_Size'].transform('size')
# 筛选出最长的连续增长段
max_growth_segment = df[sizes == sizes.max()]

3. 优化增长段判定逻辑(可选)

原逻辑允许增长段内出现持平(仅当数值下降时才分段),若需要严格连续增长(每一个数据点都比前一个大),可将lt(0)改为le(0):

# 严格连续增长:持平或下降时触发分段
df['is_increasing'] = df['Embryo_Size'].diff().le(0).cumsum()

完整可运行示例

from pandas import DataFrame

# 模拟业务数据结构
totaldata = {
    1: [[0, 10], [1, 12], [2, 15], [3, 14], [4, 16], [5, 18], [6, 20]]
}
tempvideocount = 1

# 创建DataFrame
df = DataFrame(totaldata[tempvideocount], columns=['Time', 'Embryo_Size'])

# 划分连续增长段(允许持平)
df['is_increasing'] = df['Embryo_Size'].diff().lt(0).cumsum()
# 计算各分段长度
sizes = df.groupby('is_increasing')['Embryo_Size'].transform('size')
# 获取最长增长段
max_growth = df[sizes == sizes.max()]

# 输出结果:最长增长段及调整后的起始点
print("最长连续增长段:")
print(max_growth)
print("\n调整后的起始点:")
print(max_growth.iloc[0])

内容的提问来源于stack exchange,提问作者B0Z

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 05:30:58