2013-12-22 26 views
24

我正在嘗試與Python的東西。我想切片數列表的列表(高原)(L [1]),但我有以下錯誤信息:切片索引必須是整數或無或有__index__方法

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 
+1

檢查的鰭和登場TPE,我認爲將是花車 – M4rtini

回答

35

debutfin值是浮點值,不是整數,因爲taille是浮點數。

使這些值的整數,而不是:

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

或者,讓taille整數:

taille = int(sqrt(len(plateau))) 
相關問題