• TypeError: list indices must be integers or slices, not tuple


    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]);
  • 相关阅读:
    window.fonts
    smpt authentification 配置
    如何从思维上应对
    中文字体 英文字体
    Path Breadcrumbs
    drupal commerce app
    做视频或者什么模块开发之类的
    分页符 箭头 难看
    theme wrapper 例子
    background position 稍微深入
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13313692.html
Copyright © 2020-2023  润新知