Python自动化开发-数据类型之列表
Python 数据类型-列表
列表是Python重要的数据类型之一。接下来主要介绍列表类型的常用操作。
1.创建列表
#!/usr/bin/env python # -*- coding:utf-8 -*- #创建列表 lis01=[11,"ruby",(1,2,"haha",)] print(type(lis01)) print(lis01)
代码运行结果:
<class 'list'>
[11, 'ruby', (1, 2, 'haha')]
2.列表-切片
#!/usr/bin/env python # -*- coding:utf-8 -*- #列表:切片 lis01=[11,"ruby",(1,2,"haha",),"258","lily"] r=lis01[1:3] print(type(lis01)) print(r)
代码运行结果:
<class 'list'>
['ruby', (1, 2, 'haha')]
3.列表-索引
根据元素查找列表中的元素索引位置。
#!/usr/bin/env python # -*- coding:utf-8 -*- #列表:索引 lis01=[11,"ruby",(1,2,"haha",),"258","lily"] r=lis01.index("258") print(r)
代码运行结果:3
#!/usr/bin/env python # -*- coding:utf-8 -*- #列表:索引 lis01=[11,"ruby",(1,2,"haha",),"258","lily"] r=lis01.index(11) print(r)
代码运行结果:0
4.列表-追加
#!/usr/bin/env python # -*- coding:utf-8 -*- #列表:追加 lis01=[11,"ruby",(1,2,"haha",),"258","lily"] lis01.append("asdf") lis01.append(99) print(lis01)
代码运行结果:
[11, 'ruby', (1, 2, 'haha'), '258', 'lily', 'asdf', 99]
5.列表-随机删除
#!/usr/bin/env python # -*- coding:utf-8 -*- #列表:随机删除 lis01=[11,"ruby",(1,2,"haha",),"258","lily"] lis01.pop() print(lis01)
代码运行结果:
[11, 'ruby', (1, 2, 'haha'), '258']
6.列表-指定删除
#!/usr/bin/env python # -*- coding:utf-8 -*- #列表:删除指定字符串元素258,删除数字字符元素11. lis01=[11,"ruby",(1,2,"haha",),"258","lily"] lis01.remove(11) lis01.remove("258") print(lis01)
代码运行结果:
['ruby', (1, 2, 'haha'), 'lily']
7.列表-计算长度
#!/usr/bin/env python # -*- coding:utf-8 -*- #列表:计算列表长度,即计算列表元素的个数,就是计算列表有多少个元素值。 lis01=[11,"ruby",(1,2,"haha",),"258","lily",123] r=lis01.__len__() print(r)
代码运行结果:6
8.列表-循环
循环即可迭代
#!/usr/bin/env python # -*- coding:utf-8 -*- #列表:循环,即可迭代 lis01=[11,"ruby",(1,2,"haha",),"258","lily",123] for i in lis01: print(i)
代码运行结果:
11
ruby
(1, 2, 'haha')
258
lily
123
9.列表-包含
包含:判断列表里是否包含某个元素值,True/False。
#!/usr/bin/env python # -*- coding:utf-8 -*- #列表:包含,判断列表中是否包含某个元素值 lis01=[11,"ruby",(1,2,"haha",),"258","lily",123] r=lis01.__contains__(11) s=lis01.__contains__(12) print(r) print(s)
代码运行结果:
True
False
10.列表-count
count:计算列表中的某个元素出现的次数。
#!/usr/bin/env python # -*- coding:utf-8 -*- #列表:count,计算字符串元素"258"出现了几次 lis01=[11,"ruby",(1,2,"haha",),"258","lily",123,"ruby",11,11,"258",258] r=lis01.count("258") print(r)
代码运行结果:2
11.列表-extend
#!/usr/bin/env python # -*- coding:utf-8 -*- #列表:extend lis01=[11,"ruby",(1,2,"haha",),"258","lily",123,"ruby",11,11,"258",258] print(lis01) lis01.extend("999") print(lis01) lis01.extend("1") print(lis01) lis01.extend([11,"wsd","11",(11,22,"sdf","333",[1,2,3])]) print(lis01) lis01.extend((11,"22","edf",[1,99],)) print(lis01)
代码运行结果:
[11, 'ruby', (1, 2, 'haha'), '258', 'lily', 123, 'ruby', 11, 11, '258', 258]
[11, 'ruby', (1, 2, 'haha'), '258', 'lily', 123, 'ruby', 11, 11, '258', 258, '9', '9', '9']
[11, 'ruby', (1, 2, 'haha'), '258', 'lily', 123, 'ruby', 11, 11, '258', 258, '9', '9', '9', '1']
[11, 'ruby', (1, 2, 'haha'), '258', 'lily', 123, 'ruby', 11, 11, '258', 258, '9', '9', '9', '1', 11, 'wsd', '11', (11, 22, 'sdf', '333', [1, 2, 3])]
[11, 'ruby', (1, 2, 'haha'), '258', 'lily', 123, 'ruby', 11, 11, '258', 258, '9', '9', '9', '1', 11, 'wsd', '11', (11, 22, 'sdf', '333', [1, 2, 3]), 11, '22', 'edf', [1, 99]]
12.列表-insert
#!/usr/bin/env python # -*- coding:utf-8 -*- #列表:insert,在索引为1的元素即字符串元素ruby(居左优先)前面插入新元素999 lis01=[11,"ruby",(1,2,"haha",),"258","lily",123,"ruby",11,11,"258",258] print(lis01) lis01.insert(1,999) print(lis01)
代码运行结果:
[11, 'ruby', (1, 2, 'haha'), '258', 'lily', 123, 'ruby', 11, 11, '258', 258]
[11, 999, 'ruby', (1, 2, 'haha'), '258', 'lily', 123, 'ruby', 11, 11, '258', 258]
13.列表-reverse
#!/usr/bin/env python # -*- coding:utf-8 -*- #列表:reverse,翻转 lis01=[1,2,9,4,3] print(lis01) lis01.reverse() print(lis01)
代码运行结果:
[1, 2, 9, 4, 3]
[3, 4, 9, 2, 1]
14.列表-sort
#!/usr/bin/env python # -*- coding:utf-8 -*- #列表:sort,升序 lis01=[1,2,9,4,3] print(lis01) lis01.sort() print(lis01)
代码运行结果:
[1, 2, 9, 4, 3]
[1, 2, 3, 4, 9]