python中常用的列表类型内建函数
来源:互联网 发布:python 判断英文单词 编辑:程序博客网 时间:2024/05/21 01:27
1、list.append(obj) 向列表中添加一个对象obj
fruits = ['apple', 'pear', 'orange']>>> fruits.append('apple')>>> fruits['apple', 'pear', 'orange', 'apple']
2、list.count(obj) 返回一个对象obj在列表中出现的次数
>>> fruits.count('apple')2
3、list.extend(seq) 把序列seq的内容添加到列表中
>>> seq = ['banana', 'strawberry']>>> fruits.extend(seq)>>> fruits['apple', 'pear', 'orange', 'apple', 'banana', 'strawberry']
4、list.index(obj, i=0, j=len(list))
返回 list[k] == obj 的 k 值,并且 k 的范围在 i<=k<j;否则引发 ValueError 异常。
>>> fruits.index('orange',0, len(list))2>>> fruits.index('lemon',0, len(list))Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> fruits.index('lemon',0, len(list))ValueError: 'lemon' is not in list
5、list.insert(index, obj) 在索引量为 index 的位置插入对象obj
>>> fruits.insert(3, 'lemon')>>> fruits['apple', 'pear', 'orange', 'lemon', 'apple', 'banana', 'strawberry']
6、list.pop(index=-1) 删除并返回指定位置的对象,默认是最后一个对象
>>> fruits.pop()'strawberry'>>> fruits['apple', 'pear', 'orange', 'lemon', 'apple', 'banana']
7、list.remove(obj) 从列表中删除找到的第一个obj对象,如果不存在则返回一个ValueError错误。
>>> fruits.remove('apple')>>> fruits['pear', 'orange', 'lemon', 'apple', 'banana']>>> fruits.remove('strawberry')
Traceback (most recent call last): File "<pyshell#25>", line 1, in <module> fruits.remove('strawberry')ValueError: list.remove(x): x not in list
8、list.reverse() 原地翻转列表
>>> fruits.reverse()>>> fruits['banana', 'apple', 'lemon', 'orange', 'pear']
9、list.sort(func=None,key=None, reverse=False)
以指定的方式排序列表中的成员,如果 func 和 key 参数指定,则按照指定的方式比较各个元素,如果 reverse 标志被置为True,则列表以反序排列。
>>> fruits.sort()>>> fruits['apple', 'banana', 'lemon', 'orange', 'pear']>>> fruits.sort(reverse=True)>>> fruits['pear', 'orange', 'lemon', 'banana', 'apple']
- python中常用的列表类型内建函数
- python列表类型内建函数
- python 列表类型内建函数
- Python中常用的内建函数
- python中常用的内建函数
- Python中常用的字符串内建函数
- python中常用的字典内建函数
- python中常用的字典内建函数
- Python中常用的字符串内建函数
- Python中常用的字符串内建函数
- Python列表类型的内建函数使用实例(insert、remove、index、pop等)
- Python字符串类型的内建函数
- Python字符串类型的内建函数
- Python列表内建函数
- Python中常见类型的常用函数
- Python 几个常用的内建函数
- python常用的几个内建函数
- python中常用的内建数据结构
- Android 如何监听返回键,弹出一个退出对话框
- Spring MVC简介
- Spring下载方式
- Understanding and Configuring PPP CHAP Authentication
- 31、MySQL数据库与JDBC编程
- python中常用的列表类型内建函数
- hbase memstorelab
- 6款渐变标签彩色flash按钮
- 9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
- Flex 4.6 自带JSON的使用
- 2014-5-10-word系列
- 文件遍历排序函数
- 有序广播Ordered broadcasts的使用
- 原创控件代码共享--日期选择控件