python学习之 range 和 slice

之前学习的时候一直没有仔细学习range,都是需要用的时候才顺便学习一下,这里稍微总结一下它们的用法

range:

一些例子

来源:http://www.cnblogs.com/buro79xxd/archive/2011/05/23/2054493.html

使用python的人都知道range()函数很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节。这里记录一下range(),复习下list的slide,最后分析一个好玩儿的冒泡程序。

这里记录一下:

>>> range ( 1 , 5 ) #代表从1到5(不包含5)
[ 1 , 2 , 3 , 4 ]
>>> range ( 1 , 5 , 2 ) #代表从1到5,间隔2(不包含5)
[ 1 , 3 ]
>>> range ( 5 ) #代表从0到5(不包含5)
[ 0 , 1 , 2 , 3 , 4 ]

再看看list的操作:

array =  [ 1 , 2 , 5 , 3 , 6 , 8 , 4 ]
#其实这里的顺序标识是
[ 1 , 2 , 5 , 3 , 6 , 8 , 4 ]
( 0 1 2 3 4 5 6 )
( - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 )
 
>>> array[ 0 :] #列出0以后的
[ 1 , 2 , 5 , 3 , 6 , 8 , 4 ]
>>> array[ 1 :] #列出1以后的
[ 2 , 5 , 3 , 6 , 8 , 4 ]
>>> array[:-1]#列出-1之前的 去除最后一个或者几个字符很常用的方式
[ 1 , 2 , 5 , 3 , 6 , 8 ]
>>> array[ 3 : - 3 ] #列出3到-3之间的
[ 3 ]


那么两个[::]会是什么那?

array =  [ 1 , 2 , 5 , 3 , 6 , 8 , 4 ]
>>> array[:: 2 ]
[ 1 , 5 , 6 , 4 ]
>>> array[ 2 ::]
[ 5 , 3 , 6 , 8 , 4 ]
>>> array[:: 3 ]
[ 1 , 3 , 4 ]
>>> array[:: 4 ]
[ 1 , 6 ]
如果想让他们颠倒形成reverse函数的效果(默认步长是1,现在改为-1)
>>> array[:: - 1 ]
[ 4 , 8 , 6 , 3 , 5 , 2 , 1 ]
>>> array[:: - 2 ]
[ 4 , 6 , 5 , 1 ]

官方文档对于range的解释


The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in forloops.

class  range ( stop ) class  range ( startstop [step ] )

The arguments to the range constructor must be integers (either built-in int or any object that implements the __index__ special method). If the step argument is omitted, it defaults to 1. If thestart argument is omitted, it defaults to 0. If step is zero,ValueError is raised.

For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] <  stop.(注意区分range的步长和列表切片的区别

For a negative step, the contents of the range are still determined by the formula r[i] = start + step*i, but the constraints arei >= 0 and r[i] > stop.

range object will be empty if r[0] does not meet the value constraint. Ranges do support negative indices, but these are interpreted as indexing from the end of the sequence determined by the positive indices.

Ranges containing absolute values larger than sys.maxsize are permitted but some features (such as len()) may raiseOverflowError.

Range examples:

>>>
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(0))
[]
>>> list(range(1, 0))
[]

Ranges implement all of the common sequence operations except concatenation and repetition (due to the fact that range objects can only represent sequences that follow a strict pattern and repetition and concatenation will usually violate that pattern).

start

The value of the start parameter (or 0 if the parameter was not supplied)

stop

The value of the stop parameter

step

The value of the step parameter (or 1 if the parameter was not supplied)

The advantage of the range type over a regular list or tuple is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores thestartstop and step values, calculating individual items and subranges as needed).


range写几个排序

range写个冒泡

</pre><pre name="code" class="python">for i in range(len(a)):
	for j in range(1,len(a)-i):
		if a[j] < a[j-1]:
			a[j],a[j-1] = a[j-1],a[j]

range写个插入



for i in range(len(a)):
	for j in <span style="color:#ff0000;">range(len(a)-i-1,0,-1):</span>
		if(a[j]<a[j-1]):
			a[j],a[j-1] = a[j-1],a[j]
注意官方文档部分,这样做其实是 j = len(a)-i-1 + k(-1)

并且 j > 0

range写个选择

for i in range(len(a)):
	for j in range(i,len(a)):
		if a[j] < a[i]:
			a[j],a[i] = a[i],a[j]


关于Slice

对于list,tuple的slice(切片),计算方式是一样的,只是把() 改成了[],把中间的逗号改成了:
官方文档是这么说的
class  slice ( startstop [step ] )

Return a slice object representing the set of indices specified by range(start, stop, step). The start and step arguments default to NoneSlice objects have read-only data attributesstartstop and step which merely return the argument values (or their default). They have no other explicit functionality; however they are used by Numerical Python and other third party extensions. Slice objects are also generated when extended indexing syntax is used. For example: a[start:stop:step] ora[start:stop, i]. See itertools.islice() for an alternate version that returns an iterator.


所以



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值