零基础学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-目录



你可能感兴趣的:(python)