Python列表的常用方法
来源:互联网 发布:软件开发招聘广告 编辑:程序博客网 时间:2024/05/18 00:58
一、列表是什么?
列表是由一序列特定顺序排列的元素组成的,可以把字符串、数字、字典等都可以加入列表中。
列表中元素之间没有任何关系,列表也是自带下标的,默认从0开始。
列表是最常用的Python数据类型,可以作为一个方括号内的逗号分隔值出现。
列表的数据项不需要具有相同的类型。
1. 创建列表
只需要把头号分隔的不同的数据项使用方括号括起来即可。如:
list1=['python','abc',1000]list2=[1,2,3,4,5]list3=["a","b"]与字符串的索引一样,列表索引从0开始,列表可以进行各种操作,比如:append、index、insert、pop、remove、sort、reverse、切片等
2. 访问列表
使用下标索引来访问列表中的值,也可以使用方括号的形式获取字符。
list1=['python','abc',1000]list2=[1,2,3,4,5]list3=["a","b"]print list1[0]print list2[1:5],list2[1:5:2]
python[2, 3, 4, 5] [2, 4]2. 更新列表
对列表中的数据进行修改或者更新
list=['python','hello',1998, 2008]print listprint list[2]list[2]=2001print list[2]
['python', 'hello', 1998, 2008]199820013. 删除列表元素
可以使用del语句删除列表的元素:
list=['python','hello',1998, 2008]print listdel list[2]print list
['python', 'hello', 1998, 2008]['python', 'hello', 2008]4. 列表脚本操作符
列表对+ 和*的操作符与字符串类似,+ 用于组合列表,*用于重复列表
print len([1,2,3,4]) //求列表长度print [1,2,3]+[4,5,6] //两个列表组合print ['hello!'*4] // 列表重
print 3 in [1,2,3] // 判断元素是否存在
x=1for x in [1,2,3]: //迭代 print x
5. 列表截取4[1, 2, 3, 4, 5, 6]['hello!hello!hello!hello!']True123
list=['python','hello','yangyang']print list[2] //取出第三个元素print list[-2]//取出倒数第二个元素print list[1:]//从第二个列表截取列表
yangyanghello['hello', 'yangyang']6. 列表函数 与方法
函数:
list1=['python','hello','yangyang']list2=['python',1,2,3]print cmp(list1,list2)print len(list1)print max(list2)
13python方法:
print["append"* 10]a=['python','hello','yangyang',1,2,3]print (a)a.append('ssss')print (a)print ['index'*10]print (a.index('yangyang'))print (a)a.pop()print aprint (a)a.sort()print (a)a.reverse()print (a)a.insert(3,'dddd')print (a)
['appendappendappendappendappendappendappendappendappendappend']['python', 'hello', 'yangyang', 1, 2, 3]['python', 'hello', 'yangyang', 1, 2, 3, 'ssss']['indexindexindexindexindexindexindexindexindexindex']2['python', 'hello', 'yangyang', 1, 2, 3, 'ssss']['python', 'hello', 'yangyang', 1, 2, 3]['python', 'hello', 'yangyang', 1, 2, 3][1, 2, 3, 'hello', 'python', 'yangyang']['yangyang', 'python', 'hello', 3, 2, 1]['yangyang', 'python', 'hello', 'dddd', 3, 2, 1]
阅读全文
0 0
- Python列表的常用方法
- python中列表的常用方法
- python中列表的常用方法
- Python中列表常用的方法
- python中列表的常用方法
- python---列表list常用方法
- python之 列表常用方法
- 列表常用的方法
- python字符串、元组和列表常用的一些方法
- Python列表操作和字符串的常用方法
- python序列之列表对象的常用方法
- python 列表的方法
- Python列表的方法
- Python列表常用方法大总结
- 一个列表常用的方法
- python列表的排序方法
- python 的常用方法
- 使用 Python 获取两个列表的交集、并集、差集的常用方法
- Codeforces626F-Group Projects-神奇状态之DP
- Java实现文件上传
- MCU编译与运行浅谈
- 2017
- 变色的DNA-Dijkstra模板
- Python列表的常用方法
- Linux系统上安装nginx
- [noi2012]Vigenère 密码
- 谦先生日志之身边大数据同事的日常幸福生活
- python-matplotlib绘制各种子图
- 讲给Android程序员看的前端教程(06)——HTML5标签(5)
- JZOJ5414. 【NOIP2017提高A组集训10.22】幸运值
- 一个著名的日志系统是怎么设计出来的?
- log4j:WARN Please initialize the log4j system properly解决办法