1. 程式人生 > >list indices must be integers or slices, not tuple

list indices must be integers or slices, not tuple

以下兩種情況都會出現此錯誤:

points = [
    [1, 2],
[0, 4],
[2, 0][12,1]
]
list的維數必須一致:正確寫法:
points = [
    [1, 2],
[0, 4],
[2, 0]
]
這個也會報錯:
stations = ['Schagen', 'Heerhugowaard', 'Alkmaar', 'Castricum', 'Zaandam', 'Amsterdam', 'Sloterdijk',
'Amsterdam Centraal', 'Amsterdam Amstel', 'Utrecht Centraal', '’s-Hertogenbosch'
, 'Eindhoven', 'Weert', 'Roermond', 'Sittard', 'Maastricht'] IndEind = stations.index("Heerhugowaard") IndBegin = stations.index('Sloterdijk') intBegin = int(IndBegin) intEind = int(IndEind) print('stations[0]: ', stations[intBegin, intEind])
這個是因為讀取的是時候維數錯誤:

正確寫法:

print('stations[0]: ', stations[intBegin:intEind])