码迷,mamicode.com
首页 > 编程语言 > 详细

Python 数组/列表

时间:2017-05-29 15:58:31      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:font   names   元素   数组   print   bsp   class   int   nbsp   

范围取值

指定范围

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 数组/列表

标签:font   names   元素   数组   print   bsp   class   int   nbsp   

原文地址:http://www.cnblogs.com/Gimaaa/p/6918078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!