【问题标题】:list indices must be integers or slices, not tuple列表索引必须是整数或切片,而不是元组
【发布时间】:2016-09-07 07:22:16
【问题描述】:

尝试制作我的第一个程序之一。尝试转换为int,没有成功。

收到此错误:

列表索引必须是整数或切片,而不是元组

stations = ['Schagen', 'Heerhugowaard', 'Alkmaar', 'Castricum', 'Zaandam', 'Amsterdam', 'Sloterdijk', 'Amsterdam Centraal', 'Amsterdam Amstel', 'Utrecht Centraal', '’s-Hertogenbosch', 'Eindhoven', 'Weert', 'Roermond', 'Sittard', 'Maastricht']

IndEind = stations.index(eindStation)
IndBegin = stations.index(beginStation)

intBegin = int(IndBegin)
intEind = int(IndEind)

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

【问题讨论】:

  • 我认为这会起作用print('stations[0]: ', stations[intBegin: intEind])
  • 你能给我一个输出的例子,以便我能帮忙

标签: list python-3.x


【解决方案1】:

提供print('stations[0]: ', stations[intBegin: intEind]) 而不是print('stations[0]: ', stations[intBegin, intEind]) 以详细了解Python 的切片表示法,请查看:Explain Python's slice notation

要在单独的行上打印,请给出:

for i in stations[intBegin:intEind]:
    print(i)

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-02
    • 2019-09-15
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    相关资源
    最近更新 更多