两道面试题

fi = []
for i in range(3):
    def foo(x):print(x + i) #因为函数在这时尚未执行,在这里的i,指向的仍是同一个IP地址,因此都是2.
    fi.appent(foo)
for f in fi:
    f(2)
答案:4,4,4

 

a = [0, 1, 2, 3, 4] print(a[-6:6])    #[0, 1, 2, 3, 4] print(a[6:-6])    #[]

 

切片:咱们基本上都知道Python的序列对象都是能够用索引号来引用的元素的,索引号能够是正数由0开始从左向右,也能够是负数由-1开始从右向左。python

一般一个切片操做要提供三个参数 [start_index:  stop_index:  step]  
start_index是切片的起始位置
stop_index是切片的结束位置(不包括),顾头不顾尾。
step能够不提供,默认值是1,步长值不能为0,否则会报错ValueError。

当 step 是正数时,以list[start_index]元素位置开始, step作为步长到list[stop_index]元素位置(不包括)为止,从左向右截取,
start_index和stop_index不管是正数仍是负数索引仍是混用均可以,可是要保证 list[stop_index]元素的【逻辑】位置
程序员

必须在list[start_index]元素的【逻辑】位置右边,不然取不出元素。
ruby

当 step 是负数时,以list[start_index]元素位置开始, step作为步长到list[stop_index]元素位置(不包括)为止,从右向左截取,
start_index和stop_index不管是正数仍是负数索引仍是混用均可以,可是要保证 list[stop_index]元素的【逻辑】位置
必须在list[start_index]元素的【逻辑】位置左边,不然取不出元素。
假设list的长度(元素个数)是length, start_index和stop_index在符合虚拟的逻辑位置关系时,
start_index和stop_index的绝对值是能够大于length的。
例如上面那道题。
app

Python中切片操做的实现机制

(注:Python中先后双下划线名字的方法(函数)叫特殊方法,也有称魔术方法的,这是从ruby那里借用的。
一般特殊方法都是应当由解释器去调用的,对程序员的接口一般是看起来更简洁的方式,如常见的 len(list)
实质是解释器调用list.__len__()方法。)


实际上在Python中对list引用元素和形式优雅简洁的切片操做都是由解释器调用的list.__getitem__(x)特殊方法。
>>> help(list.__getitem__)
Help on method_descriptor:


__getitem__(...)
    x.__getitem__(y) <==> x[y]


其中x能够是个整数对象或切片对象。


alist[5] 和 alist.__getitem__(5) 是彻底等效的。
>>> alist[5]
5
>>> alist.__getitem__(5)
5
>>>


而切片操做是把切片对象做参数调用__getitem__(),
>>> help(slice)
Help on class slice in module builtins:


class slice(object)
 |  slice(stop)
 |  slice(start, stop[, step])
 |  
 |  Create a slice object.  This is used for extended slicing (e.g. a[0:10:2]).


见下面的例子。


>>> alist[1:7:2]
[1, 3, 5]
>>> slice_obj = slice(1,7,2)
>>> alist.__getitem__(slice_obj)
[1, 3, 5]
>>>


一些经常使用的切片操做

>>> alist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


# 取前一部分
>>> alist[:5]
[0, 1, 2, 3, 4]


# 取后一部分
>>> alist[-5:]
[5, 6, 7, 8, 9]


# 取偶数位置元素
>>> alist[::2]
[0, 2, 4, 6, 8]


# 取奇数位置元素
>>> alist[1::2]
[1, 3, 5, 7, 9]


# 浅复制,等价于list.copy()更加面向对象的写法
>>> blist = alist[:]
>>> blist
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


# 返回一个逆序列表,推荐reversed(list)的写法,更直观易懂。
>>> alist[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]


# 在某个位置插入多个元素
>>> alist[3:3] = ['a','b','c']
>>> alist
[0, 1, 2, 'a', 'b', 'c', 3, 4, 5, 6, 7, 8, 9]


# 在开始位置以前插入多个元素
>>> alist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> alist[:0] = ['a','b','c']
>>> alist
['a', 'b', 'c', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


# 替换多个元素
>>> alist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> alist[0:3] = ['a','b','c']
>>> alist
['a', 'b', 'c', 3, 4, 5, 6, 7, 8, 9]


# 删除切片
>>> alist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> del alist[3:6]
>>> alist
[0, 1, 2, 6, 7, 8, 9]
函数

相关文章
相关标签/搜索