【问题标题】:Slice indices must be integers or None or have __index__ method切片索引必须是整数或 None 或具有 __index__ 方法
【发布时间】:2014-01-11 01:25:03
【问题描述】:

我正在尝试使用 Python。我想在几个列表(L[i])中分割一个列表(高原),但我有以下错误消息:

  File "C:\Users\adescamp\Skycraper\skycraper.py", line 20, in <module>
    item = plateau[debut:fin]
TypeError: slice indices must be integers or None or have an __index__ method

相关行是item = plateau[debut:fin]的行

from math import sqrt

plateau = [2, 3, 1, 4, 1, 4, 2, 3, 4, 1, 3, 2, 3, 2, 4, 1]

taille = sqrt(len(plateau))

# Division en lignes
L = []
i = 1
while i < taille:
    fin = i * taille
    debut = fin - taille
    item = plateau[debut:fin]
    L.append(item)
    i += 1

【问题讨论】:

  • 检查鳍和首次亮相的tpe,我认为这将是浮动

标签: python slice


【解决方案1】:

您的 debutfin 值是浮点值,而不是整数,因为 taille 是浮点数。

将这些值改为整数:

item = plateau[int(debut):int(fin)]

或者,将taille 设为整数:

taille = int(sqrt(len(plateau)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 2015-04-01
    • 1970-01-01
    • 2017-10-16
    • 2018-06-06
    • 2018-09-21
    • 2020-07-01
    相关资源
    最近更新 更多