【发布时间】:2016-07-21 03:51:07
【问题描述】:
可以像这样对 python 列表进行切片:
>>> list=['a', 'b']
>>> list[0:1]
['a']
但是,将索引作为字符串传递时,会抛出错误:
>>> index="0:1"
>>> list[index]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
如何将列表索引指定为字符串? list[0:1] 中的 0:1 到底是什么数据类型?
【问题讨论】:
-
您应该使用两个整数作为开始和停止索引,然后使用它们。
-
从traceback可以看出,“列表索引必须是整数,而不是str”
标签: python