1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python数据类型:序列(字符串 元组 列表 字典)

python数据类型:序列(字符串 元组 列表 字典)

时间:2020-10-18 16:27:44

相关推荐

python数据类型:序列(字符串 元组 列表 字典)

序列通常有2个特点:

1,可以根据索引取值

2,可以切片操作

字符串,元组,列表,字典,都可以看做是序列类型

我的操作环境:Ubuntu16.04+python2.7

一、字符串类型

>按索引获取,索引从0开始

1 >>> name='ghostwu'2 >>> name[0]3 'g'4 >>> name[1]5 'h'6 >>> name[6]7 'u'8 >>>

>切片操作,第1个冒号的值,表示从哪个索引开始切片。第2个冒号的值,表示从到哪个索引结束(注意:结果不包含这个位置)。第3个冒号的值,表示步长

>>> name='My Name Is Ghostwu'>>> name[0:7]'My Name'>>> name[0:7:1]'My Name'>>> name[0:7:2]'M ae'

默认切片操作为:从左到右。如果步长为负数,表示从右往左切片。从后往前数(索引从-1开始), type的作用:查看数据类型。

1 >>> name='My Name Is Ghostwu' 2 >>> name[-1] 3 'u' 4 >>> name[-1:-4] 5 '' 6 >>> name[-1:-4:-1] 7 'uwt' 8 >>> type(name) 9 <type 'str'>10 >>> name[2]11 ' '12 >>> name[2:]13 ' Name Is Ghostwu'14 >>> name[2:-1]15 ' Name Is Ghostw'16 >>>

字符串其他小技巧:

>len函数,计算长度

>>> str="ghostwu">>> len(str)7

>+号,连接字符串

>>> str="hi ">>> str2="ghostwu">>> str+str2'hi ghostwu'

>*号,重复字符串次数,是不是很简洁,在php中要用str_repeat或者循环连接字符串

>>> str="ghostwu">>> str*2'ghostwughostwu'>>> str'ghostwu'>>>

>in: 判断元素是否在序列中

>>> str="ghostwu">>> 'g' in strTrue>>> 'x' in strFalse>>>

>max最大值,min最小值

>>> str="abc123">>> max(str)'c'>>> min(str)'1'>>>

>cmp(str1,str2) 比较序列值是否相同

1 >>> str="abc" 2 >>> str2="ab1" 3 >>> cmp(str,str2) 4 1 5 >>> cmp(str2,str) 6 -1 7 >>> str2="abc" 8 >>> cmp(str,str2) 9 010 >>>

二、元组类型

用名称=(item,item,)小括号定义,只有一项的时候,要加逗号

字符串的痛点:如果用一个字符串,保存某个人的信息,那么在切片的时候(如人名,年龄,性别)就不太好操作

1 >>> userinfo="ghostwu 20 male"2 >>> type(userinfo)3 <type 'str'>4 >>> userinfo[0:7]5 'ghostwu'

如果用元组来处理

1 >>> userinfo=("ghostwu","20","male") 2 >>> type(userinfo) 3 <type 'tuple'> 4 >>> userinfo[0] 5 'ghostwu' 6 >>> userinfo[1] 7 '20' 8 >>> userinfo[2] 9 'male'10 >>>

看,是不是非常简单?只有一项时?怎么定义?

>>> userinfo=("ghostwu")>>> type(userinfo)<type 'str'>>>>

像上面这种定义方式,定义的是一个字符串类型。只有一项时,需要在后面加个逗号','

>>> userinfo=('ghostwu',)>>> type(userinfo)<type 'tuple'>>>> userinfo[0]'ghostwu'>>>

元组定义之后,不可以被修改:

>>> userinfo=("ghostwu",20,"male")>>> userinfo[0]="zhangsan"Traceback (most recent call last):File "<stdin>", line 1, in <module>TypeError: 'tuple' object does not support item assignment>>>

可以使用类似es6的解构语法,把元组的每一项对应赋值给左边的变量:

>>> userinfo=('ghostwu',20,'male')>>> name,age,sex=userinfo>>> name'ghostwu'>>> age20>>> sex'male'

三、列表(list)

>中括号定义

>>> list1=[]>>> type(list1)<type 'list'>

>>> userinfo=['ghostwu',20,'male']>>> type(userinfo)<type 'list'>>>> userinfo[0]'ghostwu'>>> userinfo[1]20>>> userinfo[2]'male'

>列表的切片操作

1 >>> userinfo=['ghostwu',20,'male'] 2 >>> userinfo[0:1] 3 ['ghostwu'] 4 >>> userinfo[0:2] 5 ['ghostwu', 20] 6 >>> userinfo[::2] 7 ['ghostwu', 'male'] 8 >>> userinfo[::] 9 ['ghostwu', 20, 'male']10 >>> userinfo[::1]11 ['ghostwu', 20, 'male']

>列表可以被重新赋值,列表项可以被修改,但是不能动态索引方式增加,否则报错(索引超出上限)

1 >>> userinfo=['ghostwu',20,'male'] 2 >>> len(userinfo) 3 3 4 >>> userinfo='zhangsan' 5 >>> len(userinfo) 6 8 7 >>> userinfo=[] 8 >>> len(userinfo) 9 010 >>> userinfo[0]="ghostwu"11 Traceback (most recent call last):12 File "<stdin>", line 1, in <module>13 IndexError: list assignment index out of range14 >>> userinfo=["ghostwu",20,"male"]15 >>> userinfo[0]="zhangsan"16 >>> userinfo17 ['zhangsan', 20, 'male']18 >>> userinfo[3]="china"19 Traceback (most recent call last):20 File "<stdin>", line 1, in <module>21 IndexError: list assignment index out of range22 >>>

>列表操作:

取值:切片和索引 修改: list[] = x

>>> userinfo=['ghostwu',20,'male']>>> type(userinfo)<type 'list'>>>> userinfo[0]\... 'ghostwu'>>> userinfo[0:2]['ghostwu', 20]

修改列表的某一项时候,地址没有改变,还是列表本身

>>> userinfo=["ghostwu",20,"male"]>>> id(userinfo)140648386293128>>> userinfo[0]="hello">>> id(userinfo)140648386293128

元组重新被赋值,相当于重新定义了一个新的元组:

>>> userinfo=("ghostwu",20)>>> type(userinfo)<type 'tuple'>>>> userinfo[0]="hello"Traceback (most recent call last):File "<stdin>", line 1, in <module>TypeError: 'tuple' object does not support item assignment>>> id(userinfo)140648386125696>>> userinfo=("zhangsan",30)>>> id(userinfo)140648386125552

添加: list.append()

1 >>> userinfo=["ghostwu",20]2 >>> userinfo3 ['ghostwu', 20]4 >>> userinfo.append("male")5 >>> userinfo6 ['ghostwu', 20, 'male']7 >>> userinfo.append("china")8 >>> userinfo9 ['ghostwu', 20, 'male', 'china']

删除: del(list[]) list.remove(list[]), 注意:remove删除的是列表中第一次出现的值

1 >>> userinfo 2 ['ghostwu', 20, 'male', 'china'] 3 >>> type(userinfo) 4 <type 'list'> 5 >>> userinfo.remove(20) 6 >>> userinfo 7 ['ghostwu', 'male', 'china'] 8 >>> userinfo.remove("china") 9 >>> userinfo10 ['ghostwu', 'male']

>>> userinfo=['ghostwu',20,'ghostwu','male','ghostwu']>>> userinfo['ghostwu', 20, 'ghostwu', 'male', 'ghostwu']>>> userinfo.remove('ghostwu')>>> userinfo[20, 'ghostwu', 'male', 'ghostwu']

1 >>> userinfo 2 [20, 'ghostwu', 'male', 'ghostwu'] 3 >>> type(userinfo) 4 <type 'list'> 5 >>> userinfo.remove('male') 6 >>> userinfo 7 [20, 'ghostwu', 'ghostwu'] 8 >>> del( userinfo[1] ) 9 >>> userinfo10 [20, 'ghostwu']

查找: var in list

1 >>> userinfo2 [20, 'ghostwu']3 >>> 20 in userinfo4 True5 >>> '20' in userinfo6 False7 >>> 'ghostwu' in userinfo8 True

四、字典

他的用法类似于javascript中的json,大括号中用键值对定义,取数据用对应的键

1 >>> userinfo={'name':'ghostwu', 1 : 20, 'age' : 20, 'sex' : 'male' } 2 >>> type(userinfo) 3 <type 'dict'> 4 >>> userinfo 5 {1: 20, 'age': 20, 'name': 'ghostwu', 'sex': 'male'} 6 >>> userinfo['name'] 7 'ghostwu' 8 >>> userinfo['age'] 9 >>> userinfo[1]11 20

字典中的键,可以是字符串,也可以是变量

1 >>> a=102 >>> b=203 >>> dic={a:'ghostwu','b':'male'}4 >>> dic5 {10: 'ghostwu', 'b': 'male'}6 >>> dic[10]7 'ghostwu'8 >>> dic['a']

用类似javascript的for ... in语法 遍历字典:

1 >>> userinfo={'name':'ghostwu','age':20,'sex':'male'} 2 >>> for key in userinfo: 3 ...print key 4 ... 5 age 6 name 7 sex 8 >>> for key in userinfo: 9 ...print userinfo[key]10 ... 11 ghostwu13 male14 >>>

为字典增加一项值

1 >>> userinfo2 {'age': 20, 'name': 'ghostwu', 'sex': 'male'}3 >>> type(userinfo)4 <type 'dict'>5 >>> userinfo['email']='test@'6 >>> userinfo7 {'email': 'test@', 'age': 20, 'name': 'ghostwu', 'sex': 'male'}

字典相关操作方法: del可以删除某一项,或者删除整个字典,dict.clear()是清空整个字典. dict.pop( key ),删除字典中对应的key和值,并返回被删除的值

>>> userinfo{'email': 'test@', 'age': 20, 'name': 'ghostwu', 'sex': 'male'}>>> type(userinfo)<type 'dict'>>>> userinfo['age']=30>>> userinfo{'email': 'test@', 'age': 30, 'name': 'ghostwu', 'sex': 'male'}>>> del(userinfo['age'])>>> userinfo{'email': 'test@', 'name': 'ghostwu', 'sex': 'male'}>>> userinfo.pop('email')'test@'>>> userinfo{'name': 'ghostwu', 'sex': 'male'}>>> userinfo.clear()>>> userinfo{}>>> del(userinfo)>>> userinfoTraceback (most recent call last):File "<stdin>", line 1, in <module>NameError: name 'userinfo' is not defined>>>

字典有很多的方法,比如:keys获取所有的键,values:获取所有的值

1 >>> userinfo={'name':'ghostwu','age':20,'sex':'male'}2 >>> userinfo.keys()3 ['age', 'name', 'sex']4 >>> userinfo.values()5 [20, 'ghostwu', 'male']

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。