Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2115540
  • 博文数量: 333
  • 博客积分: 10161
  • 博客等级: 上将
  • 技术积分: 5238
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-19 08:59
文章分类

全部博文(333)

文章存档

2017年(10)

2014年(2)

2013年(57)

2012年(64)

2011年(76)

2010年(84)

2009年(3)

2008年(37)

分类: LINUX

2013-12-22 20:09:43

list函数:

将字符串转化成列表,例:

[html] view plaincopy
  1. >>> name = list('hello')  
  2. >>> name  
  3. ['h', 'e', 'l', 'l', 'o']  
[html] view plaincopy
  1. <pre>pre>  
  2. <pre>pre>  
  3. <pre>pre>  
  4. <pre>pre>  
  5. <pre>pre>  
  6. <pre>pre>  


列表基本函数:

1、改变列表:元素赋值

使用索引标记

[html] view plaincopy
  1. >>>x = [1, 1, 1]  
  2. >>>x[1] = 2  
  3. >>>x  
  4. [1, 2, 1]  


2、删除元素

del语句实现

[html] view plaincopy
  1. >>>names = ['one', 'two', 'three']  
  2. >>>del names[1]  
  3. >>>names  
  4. ['one', 'three']  


3、分片赋值(第一个参数是开始分片的起始位置,第二个参数是结束分片的下一个位置)

修改序列

[html] view plaincopy
  1. >>>name = list('Perl')  
  2. >>>name[1:] = list('ython')  
  3. >>>name  
  4. ['P', 'y', 't', 'h', 'o', 'n']  

插入序列

[html] view plaincopy
  1. >>>num = [1, 5]  
  2. >>>num[1:1] = [2, 3, 4]  
  3. >>>num  
  4. [1, 2, 3, 4, 5]  

删除序列

[html] view plaincopy
  1. >>>num = [1, 5]  
  2. >>>num[1:1] = [2, 3, 4]  
  3. >>>num  
  4. [1, 2, 3, 4, 5]  

4、append函数(改变原列表)(可以实现入栈操作)

在列表末尾添加新的对象

[html] view plaincopy
  1. >>>num = [1, 5]  
  2. >>>num[1:1] = [2, 3, 4]  
  3. >>>num  
  4. [1, 2, 3, 4, 5]  


5、count函数

统计某个元素在列表中出现的次数


[html] view plaincopy
  1. >>>['to', 'be', 'or', 'not', 'to', 'be' ].count('to')  
  2. 2  
  3.   
  4. >>>x = [[1, 2], 1, 1, [2, 1, [1, 2]]]  
  5. >>>x.count(1)  
  6. 2  
  7. >>>x.cout([1, 2])  
  8. 1  

6、extend函数(修改原序列,连接操作产生新序列)

在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)


[html] view plaincopy
  1. >>>a = [1, 2, 3]  
  2. >>>b = [4, 5, 6]  
  3. >>>a.extend(b)  
  4. >>>a  
  5. [1, 2, 3, 4, 5, 6]  

用分片操作来实现上述步骤


[html] view plaincopy
  1. >>>a = [1, 2, 3]  
  2. >>>b = [4, 5, 6]  
  3. >>>a[len(a):] = b  
  4. >>>a  
  5. [1, 2, 3, 4, 5, 6]  


7、index函数

从列表中找出某个值第一个匹配项的索引位置


[html] view plaincopy
  1. >>>num = ['one', 'two', 'three', 'four', 'five']  
  2. >>>num.index['three']  
  3. 2  
  4.   
  5. >>>num[2]  
  6. 'three'  


8、insert函数

将对象插入列表


[html] view plaincopy
  1. >>> numbers = [1, 2, 3, 5, 6, 7]  
  2. >>> numbers.insert(3, 'four')  
  3. >>> numbers  
  4. [1, 2, 3, 'four', 5, 6, 7]  

9、pop函数(可以实现出栈操作)

移除列表中的一个元素(默认最后一个元素),并且返回该元素的值


[html] view plaincopy
  1. >>> x = [1, 2, 3]  
  2. >>> x.pop()  
  3. 3  
  4. >>> x  
  5. [1, 2]  
  6.   
  7. >>> x.pop(0)  
  8. 1  
  9. >>> x  
  10. [2]  



10、remove函数(和pop区别,修改原序列,但不返回)

移除列表中某个值的第一个匹配项


[html] view plaincopy
  1. >>> x = ['to', 'be', 'or', 'not', 'to', 'be']  
  2. >>> x.remove('be')  
  3. >>> x  
  4. ['to', 'or', 'not', 'to', 'be']  


11、reverse函数

反向列表中元素


[html] view plaincopy
  1. >>> x = [1, 2, 3]  
  2. >>> x.reverse()  
  3. >>> x  
  4. [3, 2, 1]  


12、sort函数(改变原列表,但是不返回)

对原列表进行排序


[html] view plaincopy
  1. >>> x = [4, 6, 2, 3, 5, 1]  
  2. >>> x.sort()  
  3. >>> x  
  4. [1, 2, 3, 4, 5, 6]  

若不需要修改原序列(注意:如果这里采用x = y的话,那么x和y都指向同一个列表,即使操作时只对y排序,实际上x也会被排序)


方法一:创建副本y,并对y进行排序


[html] view plaincopy
  1. >>> x = [4, 6, 2, 3, 5, 1]  
  2. >>> y = x[:]  
  3. >>> y.sort()  
  4. >>> x  
  5. [4, 6, 2, 3, 5, 1]  
  6. >>> y  
  7. [1, 2, 3, 4, 5, 6]  
方法二:使用sorted函数(返回一个已排序的副本,不改变原序列)



[html] view plaincopy
  1. >>> x = [4, 6, 2, 3, 5, 1]  
  2. >>> y = sorted(x)  
  3. >>> y  
  4. [1, 2, 3, 4, 5, 6]  
  5. >>> x  
  6. [4, 6, 2, 3, 5, 1]  

关键字排序:key

长度(len)排序:


[html] view plaincopy
  1. >>> x = ['bb', 'eeeee', 'a', 'dddd', 'ccc']  
  2. >>> x.sort(key = len)  
  3. >>> x  
  4. ['a', 'bb', 'ccc', 'dddd', 'eeeee']  
关键字排序:reverse(简单布尔排序,True从大到小, False从小到大)



[html] view plaincopy
  1. >>> x = [4, 6, 2, 3, 5, 1]  
  2. >>> x.sort(reverse = True)  
  3. >>> x  
  4. [6, 5, 4, 3, 2, 1]  
  5. >>> y = [4, 6, 2, 3, 5, 1]  
  6. >>> y.sort(reverse = False)  
  7. >>> y  
  8. [1, 2, 3, 4, 5, 6]  



13、cmp(x, y)函数(可以自定义compare(x, y)函数,用来比较两个元素)

比较两个函数的大小


[html] view plaincopy
  1. >>> cmp(43, 62)  
  2. -1  
  3. >>> cmp(64, 23)  
  4. 1  
  5. >>> cmp(23, 23)  
  6. 0  
结合sort函数,可以自定义排序,sort(cmp)
阅读(1224) | 评论(0) | 转发(0) |
0

上一篇:Python列表copy

下一篇:python getattr ,setattr

给主人留下些什么吧!~~