【Python操作基础】——序列

🍉CSDN小墨&晓末:https://blog.csdn.net/jd1813346972

个人介绍: 研一|统计学|干货分享
         擅长Python、Matlab、R等主流编程软件
         累计十余项国家级比赛奖项,参与研究经费10w、40w级横向

文章目录

  • [1 支持索引](#1 支持索引)
  • [2 支持切片](#2 支持切片)
  • [3 支持迭代](#3 支持迭代)
  • [4 支持拆包](#4 支持拆包)
  • [5 支持*运算](#5 支持*运算)
  • [6 通用函数](#6 通用函数)

【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)]
相关推荐
测试老哥4 分钟前
外包干了两年,技术退步明显。。。。
自动化测试·软件测试·python·功能测试·测试工具·面试·职场和发展
終不似少年遊*6 分钟前
美国加州房价数据分析01
人工智能·python·机器学习·数据挖掘·数据分析·回归算法
ue星空31 分钟前
Windbg常用命令
windows
如若12334 分钟前
对文件内的文件名生成目录,方便查阅
java·前端·python
西猫雷婶1 小时前
python学opencv|读取图像(二十一)使用cv2.circle()绘制圆形进阶
开发语言·python·opencv
yuanManGan1 小时前
数据结构漫游记:静态链表的实现(CPP)
数据结构·链表
brrdg_sefg1 小时前
WEB 漏洞 - 文件包含漏洞深度解析
前端·网络·安全
老刘莱国瑞2 小时前
STM32 与 AS608 指纹模块的调试与应用
python·物联网·阿里云
火星机器人life2 小时前
基于ceres优化的3d激光雷达开源算法
算法·3d