Python:列表函数
来源:互联网 发布:python算法 编辑:程序博客网 时间:2024/06/05 05:59
list函数:
将字符串转化成列表,例:
>>> name = list('hello')>>> name['h', 'e', 'l', 'l', 'o']
列表基本函数:
1、改变列表:元素赋值
使用索引标记
>>>x = [1, 1, 1]>>>x[1] = 2>>>x[1, 2, 1]
2、删除元素
del语句实现
>>>names = ['one', 'two', 'three']>>>del names[1]>>>names['one', 'three']
3、分片赋值(第一个参数是开始分片的起始位置,第二个参数是结束分片的下一个位置)
修改序列
>>>name = list('Perl')>>>name[1:] = list('ython')>>>name['P', 'y', 't', 'h', 'o', 'n']插入序列
>>>num = [1, 5]>>>num[1:1] = [2, 3, 4]>>>num[1, 2, 3, 4, 5]删除序列
>>>num = [1, 5]>>>num[1:1] = [2, 3, 4]>>>num[1, 2, 3, 4, 5]
4、append函数(改变原列表)(可以实现入栈操作)
在列表末尾添加新的对象
>>>num = [1, 5]>>>num[1:1] = [2, 3, 4]>>>num[1, 2, 3, 4, 5]
5、count函数
统计某个元素在列表中出现的次数
>>>['to', 'be', 'or', 'not', 'to', 'be' ].count('to')2>>>x = [[1, 2], 1, 1, [2, 1, [1, 2]]]>>>x.count(1)2>>>x.cout([1, 2])16、extend函数(修改原序列,连接操作产生新序列)
在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
>>>a = [1, 2, 3]>>>b = [4, 5, 6]>>>a.extend(b)>>>a[1, 2, 3, 4, 5, 6]用分片操作来实现上述步骤
>>>a = [1, 2, 3]>>>b = [4, 5, 6]>>>a[len(a):] = b>>>a[1, 2, 3, 4, 5, 6]7、index函数
从列表中找出某个值第一个匹配项的索引位置
>>>num = ['one', 'two', 'three', 'four', 'five']>>>num.index['three']2>>>num[2]'three'
8、insert函数
将对象插入列表
>>> numbers = [1, 2, 3, 5, 6, 7]>>> numbers.insert(3, 'four')>>> numbers[1, 2, 3, 'four', 5, 6, 7]9、pop函数(可以实现出栈操作)
移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
>>> x = [1, 2, 3]>>> x.pop()3>>> x[1, 2]>>> x.pop(0)1>>> x[2]
10、remove函数(和pop区别,修改原序列,但不返回)
移除列表中某个值的第一个匹配项
>>> x = ['to', 'be', 'or', 'not', 'to', 'be']>>> x.remove('be')>>> x['to', 'or', 'not', 'to', 'be']11、reverse函数
反向列表中元素
>>> x = [1, 2, 3]>>> x.reverse()>>> x[3, 2, 1]12、sort函数(改变原列表,但是不返回)
对原列表进行排序
>>> x = [4, 6, 2, 3, 5, 1]>>> x.sort()>>> x[1, 2, 3, 4, 5, 6]
若不需要修改原序列(注意:如果这里采用x = y的话,那么x和y都指向同一个列表,即使操作时只对y排序,实际上x也会被排序)方法一:创建副本y,并对y进行排序
>>> x = [4, 6, 2, 3, 5, 1]>>> y = x[:]>>> y.sort()>>> x[4, 6, 2, 3, 5, 1]>>> y[1, 2, 3, 4, 5, 6]方法二:使用sorted函数(返回一个已排序的副本,不改变原序列)>>> x = [4, 6, 2, 3, 5, 1]>>> y = sorted(x)>>> y[1, 2, 3, 4, 5, 6]>>> x[4, 6, 2, 3, 5, 1]关键字排序:key
长度(len)排序:
>>> x = ['bb', 'eeeee', 'a', 'dddd', 'ccc']>>> x.sort(key = len)>>> x['a', 'bb', 'ccc', 'dddd', 'eeeee']关键字排序:reverse(简单布尔排序,True从大到小, False从小到大)>>> x = [4, 6, 2, 3, 5, 1]>>> x.sort(reverse = True)>>> x[6, 5, 4, 3, 2, 1]>>> y = [4, 6, 2, 3, 5, 1]>>> y.sort(reverse = False)>>> y[1, 2, 3, 4, 5, 6]13、cmp(x, y)函数(可以自定义compare(x, y)函数,用来比较两个元素)
比较两个函数的大小
>>> cmp(43, 62)-1>>> cmp(64, 23)1>>> cmp(23, 23)0结合sort函数,可以自定义排序,sort(cmp)
- Python:列表函数
- PYTHON列表常用函数
- python 列表函数
- python 列表函数
- python 列表函数
- python的列表函数
- python 列表函数
- python列表函数
- python 列表函数
- python 列表常用函数
- python 列表函数
- Python函数列表
- python 列表函数
- python学习--列表函数
- python 列表函数
- Python列表排序函数
- python 列表函数&方法
- demo_6.Python列表函数
- JUNIT COOKBOOK
- android MediaPlayer播放InputStream解决方案
- SQL Server 2005 中的分区表和索引
- usb鼠标驱动注解及测试收藏
- Linux(centos)编译安装boost
- Python:列表函数
- 以太网芯片MAC和PHY的关系
- linux2.4下DMA的使用
- SQL Server2008存储结构之堆表、行溢出 、页结构、记录结构
- animation的xml定义中的android:interpolator属性
- S3C2410读写Nand Flash分析
- 用jqueryajax类实现的一个ajax自动保存草稿功能
- Linux2.6内核驱动移植参考
- c/c++和java中关于char的长度问题