zoukankan      html  css  js  c++  java
  • 零基础学python-8.3 列表的索引、分片和矩阵

    索引:

    注意:如果超出长度,则报错

    如果索引为负数,则取数的方向反转

    >>> aList=['123',123,123.0]
    >>> aList[0]
    '123'
    >>> aList[2]
    123.0
    >>> aList[-2]
    123
    >>> aList[-3]
    '123'
    >>> aList[3]
    Traceback (most recent call last):
      File "<pyshell#25>", line 1, in <module>
        aList[3]
    IndexError: list index out of range


    分片:

    注意:如果是负数,取值会出现变化

    >>> aList=['123',123,123.0]
    >>> aList[0:1]
    ['123']
    >>> aList[1:3]
    [123, 123.0]
    >>> aList[0:4]
    ['123', 123, 123.0]
    >>> aList[-1:4]
    [123.0]
    >>> aList[-1:0]
    []
    >>> aList[-1:1]
    []
    >>> 

    >>> aList[-1:5]
    [123.0]
    >>> aList[-1:7]
    [123.0]
    >>> aList[-3:4]
    ['123', 123, 123.0]
    >>> 


    矩阵:类似于java的多维数组

    >>> matrix=[[1,2,3],['a','b'],[('ttt')]]
    >>> matrix[1]
    ['a', 'b']
    >>> matrix[2][0]
    'ttt'
    >>> matrix[1][1]
    'b'
    >>> 

    就说到这里,谢谢大家

    ------------------------------------------------------------------

    点击跳转零基础学python-目录



    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    SpringBoot详解(二)——
    SpringBoot详解(一)——
    数据库三大范式
    Mysql备份
    mysql索引
    mysql事务
    几种数据库查找的案例
    点击加载更多
    layer、弹出框
    验证码倒计时
  • 原文地址:https://www.cnblogs.com/raylee2007/p/4774474.html
Copyright © 2011-2022 走看看