【Python操作基础】系列——序列操作,建议收藏!
该篇文章首先利用Python展示了使用序列类型数据的相关操作,包括索引、切片、迭代、拆包、*运算、通用函数操作实例演示等。
1 支持索引
运行程序:
myString="123456789" myString[1] myList=[11,12,13,14,15,16,17,18,19] myList[1] myTuple=(21,22,23,24,25,26,27,28,29) myTuple[1]
运行结果:
'2' 12 22
2 支持切片
运行程序:
myString="123456789" myString[1:9:2] #步长为2切片 myList=[11,12,13,14,15,16,17,18,19] myList[1:9:2] myTuple=(21,22,23,24,25,26,27,28,29) myTuple[1:9:2]
运行结果:
'2468' [12, 14, 16, 18] (22, 24, 26, 28)
3 支持迭代
运行程序:
from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = "all" ##执行多输出 myString="123456789" for i in myString: print(i,end=" ") myList=[11,12,13,14,15,16,17,18,19] for i in myList: print(i,end=" ") myTuple=(21,22,23,24,25,26,27,28,29) for i in myTuple: print(i,end=" ")
运行结果:
1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 21 22 23 24 25 26 27 28 29
4 支持拆包
运行程序:
myString="123456789" a1,a2,a3,a4,a5,a6,a7,a8,a9=myString a1,a2,a3,a4,a5,a6,a7,a8,a9 myList=[11,12,13,14,15,16,17,18,19] a1,a2,a3,a4,a5,a6,a7,a8,a9=myList a1,a2,a3,a4,a5,a6,a7,a8,a9 myTuple=(21,22,23,24,25,26,27,28,29) a1,a2,a3,a4,a5,a6,a7,a8,a9=myTuple a1,a2,a3,a4,a5,a6,a7,a8,a9
运行结果:
('1', '2', '3', '4', '5', '6', '7', '8', '9') (11, 12, 13, 14, 15, 16, 17, 18, 19) (21, 22, 23, 24, 25, 26, 27, 28, 29)1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 21 22 23 24 25 26 27 28 29
5 支持*运算
运行程序:
myString="123456789" myString * 3 #重复运算符 myList=[11,12,13,14,15,16,17,18,19] myList * 3 myTuple=(21,22,23,24,25,26,27,28,29) myTuple * 3
运行结果:
'123456789123456789123456789' [11, 12, 13, 14, 15, 16, 17, 18, 19, 11, 12, 13, 14, 15, 16, 17, 18, 19, 11, 12, 13, 14, 15, 16, 17, 18, 19] (21, 22, 23, 24, 25, 26, 27, 28, 29, 21, 22, 23, 24, 25, 26, 27, 28, 29, 21, 22, 23, 24, 25, 26, 27, 28, 29)
6 通用函数
运行程序:
myString="123456789" myList=[11,12,13,14,15,16,17,18,19] myTuple=(21,22,23,24,25,26,27,28,29) len(myString),len(myList),len(myTuple) sorted(myString),sorted(myList),sorted(myTuple) (['1', '2', '3', '4', '5', '6', '7', '8', '9'], [11, 12, 13, 14, 15, 16, 17, 18, 19], [21, 22, 23, 24, 25, 26, 27, 28, 29]) reversed(myString),reversed(myList),reversed(myTuple)#生成迭代器 list(reversed(myString)) #强制转换值 enumerate(myString),enumerate(myList),enumerate(myTuple) list(enumerate(myString)) [(0, '1'), (1, '2'), (2, '3'), (3, '4'), (4, '5'), (5, '6'), (6, '7'), (7, '8'), (8, '9')] zip(myList,myTuple) list(zip(myList,myTuple))
运行结果:
(9, 9, 9) (['1', '2', '3', '4', '5', '6', '7', '8', '9'], [11, 12, 13, 14, 15, 16, 17, 18, 19], [21, 22, 23, 24, 25, 26, 27, 28, 29]) (['1', '2', '3', '4', '5', '6', '7', '8', '9'], [11, 12, 13, 14, 15, 16, 17, 18, 19], [21, 22, 23, 24, 25, 26, 27, 28, 29]) (<reversed at 0x2cd42810208>, <list_reverseiterator at 0x2cd428102b0>, <reversed at 0x2cd42810128>) ['9', '8', '7', '6', '5', '4', '3', '2', '1'] (<enumerate at 0x2cd449af048>, <enumerate at 0x2cd449af090>, <enumerate at 0x2cd449af288>) [(0, '1'), (1, '2'), (2, '3'), (3, '4'), (4, '5'), (5, '6'), (6, '7'), (7, '8'), (8, '9')] [(0, '1'), (1, '2'), (2, '3'), (3, '4'), (4, '5'), (5, '6'), (6, '7'), (7, '8'), (8, '9')] <zip at 0x2cd44b38548> [(11, 21), (12, 22), (13, 23), (14, 24), (15, 25), (16, 26), (17, 27), (18, 28), (19, 29)]