Extracting a range of data from a python list
This question already has an answer here:
You should do list[44:49]
rather than list[44:5]
. Usually when you want to fetch 5 items after (including) the a+1th item, you do L[a, a+5]
. 'Usually' implies there are more flexible ways to do so: see Extended Slices: http://docs.python.org/release/2.3/whatsnew/section-slices.html.
Also try not to use list
as your list name. It overwrites list()
.
It is list[starting:ending] not list[starting:length].
So you should do list[44:49]
For more information on slice notation click here
In the slice notation [a:b]
, the second value ( b
) is not length, but a upper bound.
To get five elements starting from 44
you should use [44:49]
(49 = 44 + 5)
If upper bound is smaller than lower bound, you get empty sequence.
链接地址: http://www.djcxy.com/p/26744.html上一篇: 切片操作员理解
下一篇: 从python列表中提取一系列数据