# 数据结构—序列

1、python中常见序列

2、序列的基本操作

3、序列的转换

someone

xiaoluo

python3

python中常见序列(serial)

python中常见的序列有：字符串、列表(list)、元组(tuple)、集合(set)、字典(dict)

1) 除集合外，均可索引，索引值从0开始

2) 除集合外可以通过切片得到一个范围内元素的集合

3) 有很多共同的操作(重复操作符、拼接操作符、成员关系符等)

1) 字符串

>>> a ='The is a sample'

>>> a[]

'T'

>>> a[-1]

'e'

>>> a[3:5]

' i'

>>> a[4:6]

'is'

>>> a[-3:]

'ple'

2) 集合(set)

>>> a = {1,2,3,4,5}

>>> b = {1,2,3,4,1,2,3,6,7,8}

>>> b

{1,2,3,4,6,7,8}

>>> b = {1,2,3,4,1,2,3,6,7,8}

>>>max(b)

8

>>>min(b)

1

>>>len(b)

7

>>>sum(b)

31

1) 字符串转list: list(字符串)，转tuple: tuple(字符串)，转set: set(字符串)

>>> a ='this is a story'

>>>list(a)

['t','h','i','s',' ','i','s',' ','a',' ','s','t','o','r',

'y']

>>>tuple(a)

('t','h','i','s',' ','i','s',' ','a',' ','s','t','o','r',

'y')

>>>set(a)

{'a','h','t','o','r','s',' ','y','i'}

2) list转字符串: ''.join(字符串)，转tuple:tuple(list)，转set: set(list)

>>> a = ['the','school']

>>>''.join(a)

'theschool'

>>> a = ['1','2','3']

>>>''.join(a)

'123'

>>>tuple(a)

('1','2','3')

>>>set(a)

{'1','2','3'}

3) tuple转list：list(tuple)，转set:set(tuple)，转字符串: ''.join(tuple)

>>> a = ('1','2','3','4','5','6')

>>>list(a)

['1','2','3','4','5','6']

>>>set(a)

{'6','4','3','2','5','1'}

>>>''.join(a)

'123456'

4) set转list: list(set)，转tuple: tuple(set)，转字符串:''.join(set)

>>> a = {'1','2','3','4','5','6'}

>>>list(a)

['6','4','3','2','5','1']

>>>set(a)

{'6','4','3','2','5','1'}

>>>''.join(a)

'643251'

5) dict需要key和value值，可以转化为其他几种序列，但其他序列不可转化为dict

>>> dict = {'char':1,'list':2,'set':3,

'tuple':4,'dicy':5}

>>>str(dict)

"{'char': 1, 'list': 2, 'set': 3, 'tuple': 4,

'dicy': 5}"

>>>list(dict)

['char','list','set','tuple','dicy']

>>>set(dict)

{'char','tuple','dicy','list','set'}

>>>tuple(dict)

('char','list','set','tuple','dicy')

python基础系列目录

if语句

while语句

for循环

break语句

continue语句

return语句

https://study.163.com/course/introduction.htm?courseId=1006044085

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20181018G1Y6HC00?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 yunjia_community@tencent.com 删除。

2021-12-01

2021-12-01

2018-06-28

2018-06-13

2018-06-13

2021-12-01

2018-11-23

2018-03-13

2021-12-01

2021-12-01

2021-12-01

2021-12-01