Python入门:列表的清空

情况列表的操作:

del list[:]

list=[]

list[:]=[]

1.

[python]  view plain  copy
  1. def func(L):                                                                      
  2.     L.append(1)  
  3.     print L  
  4.     #L[:]=[]  
  5.     #del L[:]  
  6.     L = []  
  7.     print L  
  8.   
  9. L=[]  
  10. func(L)  
  11. print L  
输出结果:

[1]
[]
[1]

分析:L是可变数据类型,L作为参数,函数内对L的改变,是可以反映到函数外的L中的,执行L.append(1),是在操作,函数外L所占据的那块内存,然后执行L =[],(函数内的L),想当于L指向了另外一个空间。所以,func(L),print L,输出[1]。

其实函数的本意是将参数L指向的内存清空,用L=[],并不能清空L指向的内存

2.

[python]  view plain  copy
  1. def func(L):  
  2.     L.append(1)                                                                   
  3.     print L  
  4.     L[:]=[]  
  5.     #del L[:]  
  6.     #L = []  
  7.     print L  
  8.   
  9. L=[]  
  10. func(L)  
  11. print L  
输出结果:

[1]
[]
[]

L[:]=[]:把L对应的内存清空

3.

[python]  view plain  copy
  1. def func(L):                                                                      
  2.     L.append(1)  
  3.     print L  
  4.     #L[:]=[]  
  5.     del L[:]  
  6.     #L = []  
  7.     print L  
  8.   
  9. L=[]  
  10. func(L)  
  11. print L  
分析:

del L[:] 的效果跟L[:]=[]的效果是一样的。

python 赋值,往往是通过指针完成的,a=b,只是让a指向了b,并未把b的内容拷贝到a

4.

[python]  view plain  copy
  1. def func(L):                                                                      
  2.     L.append(1)  
  3.     print L  
  4.     print id(L)  
  5.     #L[:]=[]  
  6.     #del L[:]  
  7.     L = []  
  8.     print id(L)  
  9.     print L  
  10.   
  11. L=[]  
  12. func(L)  
  13. print L  
输出结果:

31460240
31460168

很明显:通过赋值L=[]后,L指向的内存完全不一致了。

类似于c++的引用赋值。

Python 赋值都是引用赋值,相当于使用指针来实现的另一个例证

5.

[python]  view plain  copy
  1. list =[]                                                                          
  2. next = [None,None]  
  3. for i in range(10):  
  4.     next[0] = i   
  5.     #print id(i)  
  6.     #print id(next[0])  
  7.     next[1] = i   
  8.     #print id(next)  
  9.     list.append(next)  
  10.   
  11. print list  
输出结果:

[[9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9]]

跟我们想要的结果不一致

list.append(next),仅仅是把next的地址放到list 里面

我们整个for 循环就使用了一个next,只是每次for循环,都在初始的next上进行操作,本次的操作会覆盖上次的结果

6.

[python]  view plain  copy
  1. list =[]                                                                          
  2. next = [None,None]  
  3. for i in range(10):  
  4.     next[0] = i   
  5.     #print id(i)  
  6.     #print id(next[0])  
  7.     next[1] = i   
  8.     #print id(next)  
  9.     list.append(next)  
  10.   
  11. print list  
  12. print id(list[0])  
  13. print id(list[1])  
输出结果:

[[9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9]]
36166472
36166472

解决办法,每次for 循环都重新分配空间

7.

[python]  view plain  copy
  1. list =[]                                                                          
  2. for i in range(10):  
  3.     next = [None,None]  
  4.     next[0] = i   
  5.     #print id(i)  
  6.     #print id(next[0])  
  7.     next[1] = i   
  8.     #print id(next)  
  9.     list.append(next)  
  10.   
  11. print list  
  12. print id(list[0])  
  13. print id(list[1])  

输出结果:

[[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7], [8, 8], [9, 9]]
15060360

15059712

8.

(1)大数据量的list,要进行局部元素删除,尽量避免用del随机删除,非常影响性能,如果删除量很大,不如直接新建list,然后用下面的方法释放清空旧list。

(2)对于一般性数据量超大的list,快速清空释放内存,可直接用 a = [] 来释放。其中a为list。

(3)对于作为函数参数的list,用上面的方法是不行的,因为函数执行完后,list长度是不变的,但是可以这样在函数中释放一个参数list所占内存: del a[:],速度很快,也彻底:)

(4)clear() 函数用于清空列表,类似于 del a[:]


4.
4.

猜你喜欢

转载自blog.csdn.net/m_z_g_y/article/details/80520807