列表的查

############################################################################查(从0开始)
#切片,顾头不顾尾
#只取第三个到第四个
a=['xiaoxing','xiaoliu','xiaozhang','xiaohong','xiaoli','xiaozhao']
print(a[2:4])

输出结果:
['xiaozhang', 'xiaohong']

  





#取第三个到最后
a=['xiaoxing','xiaoliu','xiaozhang','xiaohong','xiaoli','xiaozhao']
print(a[2:])


输出结果:

['xiaozhang', 'xiaohong', 'xiaoli', 'xiaozhao']

  




#从第二个开始取,到倒数第二个
a=['xiaoxing','xiaoliu','xiaozhang','xiaohong','xiaoli','xiaozhao']
print(a[1:-1])


输出结果:
['xiaoliu', 'xiaozhang', 'xiaohong', 'xiaoli']

  





#步长,从第二个开始取,到倒数第二个,取到的值再相隔2个取
a=['xiaoxing','xiaoliu','xiaozhang','xiaohong','xiaoli','xiaozhao']
print(a[1:-1:2])



输出结果:
['xiaoliu', 'xiaohong']

  


#步长,从第二个开始取,隔两个取一次
a=['xiaoxing','xiaoliu','xiaozhang','xiaohong','xiaoli','xiaozhao']
print(a[1::2])


输出结果:
['xiaoliu', 'xiaohong', 'xiaozhao']

  


#从第四个开始取值,并且从以选取的值中,第2位反向选取:
a=['xiaoxing','xiaoliu','xiaozhang','xiaohong','xiaoli','xiaozhao']
print(a[3::-1])


输出结果:
['xiaohong', 'xiaozhang', 'xiaoliu', 'xiaoxing']

  



b=['0','1','2','3','4','5','6']
print(b[4::-1]


输出结果:
['4', '3', '2', '1', '0']

  




上一篇:列表的改


下一篇:Python 初学笔记 - 第四章-类