1、错误描述
E:PycharmProjectscmnvenvScriptspython.exe E:/PycharmProjects/cmn/venv/com.you.cmn/A.py
Traceback (most recent call last):
File "E:/PycharmProjects/cmn/venv/com.you.cmn/A.py", line 15, in <module>
print(s[-7,-2]);
TypeError: list indices must be integers or slices, not tuple
dkjask
kjaskdj
j
dkjas
dkjaskdj
kjas
akadkjaskdjakadkjaskdjakadkjaskdj
6
[5, 6]
[5, 6, 7, 8]
Process finished with exit code 1
2、错误原因
在对列表进行切片时,使用的是负数print(s[-7,-2]);出现错误。
import sys;
sts = "akadkjaskdj";
s = [1,2,3,4,5,6,7,8]
print(sts[3:9]);
print(sts[4:]);
print(sts[5]);
print(sts[-8:-3]);
print(sts[-8:]);
print(sts[4:8]);
print(sts*3);
print(s[5]);
print(s[4:6])
print(s[4:])
print(s[-7,-2]);
print(s[-4]);
print(s[-4:]);
print(s*4)
3、解决办法
经过检查发现,切片使用两个数字,中间需要用:间隔
print(s[-7:-2]);