zoukankan      html  css  js  c++  java
  • python中列表切片

    python中列表切片。

    1、基本用法

    >>> test1
    [11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
    >>> test1[2:5]
    [33, 44, 55]

    2、

    >>> test1
    [11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
    >>> test1[:3]
    [11, 22, 33]
    >>> test1
    [11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
    >>> test1[7:]
    [88, 99, 0]
    >>> test1
    [11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
    >>> test1[:]
    [11, 22, 33, 44, 55, 66, 77, 88, 99, 0]

    3、

    >>> test1
    [11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
    >>> test2[-5:-2]
    [66, 77, 88]
    >>> test1
    [11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
    >>> test1[-5:]
    [66, 77, 88, 99, 0]

    4、

    >>> test1
    [11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
    >>> test1[0:10:2]
    [11, 33, 55, 77, 99]
    >>> test1[::2]
    [11, 33, 55, 77, 99]
    >>> test1[::3]
    [11, 44, 77, 0]

    5、

    >>> test1
    [11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
    >>> test1[::-1]
    [0, 99, 88, 77, 66, 55, 44, 33, 22, 11]
    >>> test1
    [11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
    >>> test1[::-2]
    [0, 88, 66, 44, 22]

    6、

    >>> test1
    [11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
    >>> test1[-1:-8:-2]
    [0, 88, 66, 44]
  • 相关阅读:
    scala
    数据结构(01)
    基本算法(07)
    基本算法(06)
    基本算法(05)
    git pull文件时和本地文件冲突的问题
    获取两个日期之间的日期形成一个集合
    lombok的简单介绍(2)
    springboot启动报错
    逆向工程的创建
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14715627.html
Copyright © 2011-2022 走看看