首页 > 代码库 > Python 数组/列表
Python 数组/列表
范围取值
指定范围
names = ["零","一","二","三","四","五","六","七"] print(names[1:4])#打印 一,二,三 左包括右不包括
从某位置取到末尾
names = ["零","一","二","三","四","五","六","七"] print(names[1:])#打印 [‘一‘, ‘二‘, ‘三‘, ‘四‘, ‘五‘, ‘六‘, ‘七‘]
从某位置取到倒数第几个
names = ["零","一","二","三","四","五","六","七"] print(names[1:-3])#打印 [‘一‘, ‘二‘, ‘三‘, ‘四‘]
跳着取值
names = ["零","一","二","三","四","五","六","七"] print(names[0::1])#打印[‘零‘, ‘一‘, ‘二‘, ‘三‘, ‘四‘, ‘五‘, ‘六‘, ‘七‘] //从头到尾一个一个取 print(names[0:-1:2])#打印[‘零‘, ‘二‘, ‘四‘, ‘六‘] //从头到倒数第二个,跳着一个一个取 print(names[0::3])#打印[‘零‘, ‘三‘, ‘六‘] //从头到尾跳着两个取
倒数取值
names = ["零","一","二","三","四","五","六","七"] print(names[5::-1])#打印[‘五‘, ‘四‘, ‘三‘, ‘二‘, ‘一‘, ‘零‘] //第5个元素到第1个元素,倒着一个一个取 print(names[6:2:-2])#打印[‘六‘, ‘四‘] //第6个元素到第2个元素,倒着跳一个一个取 print(names[-1:0:-2]
Python 数组/列表
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。