1、概念
未指定起始索引时,默认为0;未指定终止索引时,默认为列表长度。
>>> li = ['one', 'two', 'three', 'four', 'five'] >>> li[:4] # 未指定起始索引,默认为 0 # ['one', 'two', 'three', 'four'] >>> li[1:] # 未指定结束索引,默认为 列表长度 # ['two', 'three', 'four', 'five']
2、当前后索引都省略时,意味着创建一份原列表的副本
>>> li = ['one', 'two', 'three', 'four', 'five'] >>> li[:] # ['one', 'two', 'three', 'four', 'five']
3、注意,切片是原列表的一份副本,操作切片并不会对原列表产生影响
>>> li = ['one', 'two', 'three', 'four', 'five'] >>> li_slice = li[:] >>> li_slice.append('six') >>> li_slice # ['one', 'two', 'three', 'four', 'five', 'six'] >>> li # ['one', 'two', 'three', 'four', 'five']
以上就是python默认索引的介绍,希望对大家有所帮助。更多Python学习指路:
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
Copyright © 2019- net188.cn 版权所有 湘ICP备2022005869号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务