切片索引必须是整数或无或具有__index__方法

Des*_*sAu 50 python slice

我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)

有关线是与之相关的 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
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 78

debutfin值是浮点值,而不是整数,因为它taille是一个浮点数.

改为使这些值成为整数:

item = plateau[int(debut):int(fin)]
Run Code Online (Sandbox Code Playgroud)

或者,做taille一个整数:

taille = int(sqrt(len(plateau)))
Run Code Online (Sandbox Code Playgroud)