一、Python之数据类型

字符串、列表、元组、字典与集合

1. 字符串(str)

 字符串是定义在单引号\双引号\三引号内,由一串字符组成,字符串常用函数如下:
0) 定义字符串
mstr1 = ’abc‘ mstr2 = '123' mstr3 = 'a13b2'
1) 切片,按索引取值,顾头不顾尾
mstr = 'hello word' print(mstr[1:8]) #ello wo #从第2个字符到第8个字符 print(mstr[:]) #hello word #全部取出 print(mstr[1:]) #ello word #从第2个字符到最后 print(mstr[:8]) #hello wo #从第1个字符到第8个字符
2) len 计算长度
mstr = 'hello word' print(mstr) #10 一个空格也算作一个字符
3) 成员计算 in/not in
mstr = 'hello word' print( 'h' in mstr) #True print('a' not in mstr) #True
4) strip,lstrip,rstrip 移除字符串两端的空格
mstr = ' hello word ' print(mstr.strip()) #’hello word‘ 中间的空格不会去除 print(mstr.lstrip()) #'hello word ' 仅去除左边空格 print(mstr.rstrip()) #' hello word' 仅去除右边空格
5) split,rsplit 按某个字符分割
mstr = 'root:a:b:c:d' print(mstr.split(':')) #['root', 'a', 'b', 'c', 'd'] print(mstr.split(':',1)) #['root', 'a:b:c:d'] 从左分割,只分成1次 print(mstr.rsplit(':')) #['root', 'a', 'b', 'c', 'd'] 和split结果一致 print(mstr.rsplit(':',1)) #['root:a:b:c', 'd'] 从右分割,分割1次 print(mstr.rsplit(':',2)) # ['root:a:b', 'c', 'd'] 从右分割,分割2次
6) lower,upper,capitalize,swapcase,title大小写转换
mstr='AbcDe' print(mstr.lower()) #abcde 全部转为小写 print(mstr.upper()) #ABCDE 全部转为大写 print(mstr.swapcase()) #aBCdE 大写转小写,小写转大写 mstr = 'my name is test' print(mstr.capitalize()) #My name is test 标题首字母大写 print(mstr.title()) #My Name Is Test 所有单词首字母大写
7) startswith,endswith 判断是否以某个字符开头或者结尾
mstr=‘AbcDe’ print(mstr.startswith('A')) #True print(mstr.endswith('e')) #True
8) replace 字符串替换
mstr = 'hello word' print(mstr.replace('word','python')) #hello python
9) isdigit 判断字符串中是否全是数字
mstr1 = 'hello word' print(mstr.isdigit()) #False mstr2 = '1a2b' print(mstr.isdigit()) #False mstr3 = 123 print(mstr.isdigit()) #True

2. 列表(list)

列表是最常用的Python数据类型,方括号内的逗号分隔,列表相关函数如下:
0) 定义列表
list1 = [1,2,3] list2 = ['a','b','c'] list3 = ['1,2,3,['a','b','c']] 列表中的元素也可以是列表或其他数据类型
1) 更改列表元素中的值
list1 = [1,2,3] list1[0] = 2 list1 #[2,2,3]
2) 删除元素
list2 = ['a','b','c','d','e','f'] del list2[2] list2 #['a','c']
3) 分片赋值
list3=list2[1:3] list3 #['b','c']
4) append 追加到列表的最后,每次只能追加一个元素
mlist = ['aa','bb','cc','dd'] mlist.append('e') print(mlist) #['aa', 'bb', 'cc', 'dd', 'e']
5) del 删除指定元素,括号内加索引
mlist = ['aa','bb','cc','dd'] del mlist[0] print(mlist) #['bb', 'cc', 'dd']
6) remove 删除指定元素,括号内加元素名称
mlist = ['aa','bb','cc','dd'] mlist.remove('bb') print(mlist) #['aa', 'cc', 'dd']
7) pop 默认不加参数从最后一个取值
mlist = ['aa','bb','cc','dd'] res = mlist.pop(0) #pop(0) 从第一个取值 print(mlist) #['bb', 'cc', 'dd'] print(res) #aa
8) insert 在指定位置加入
mlist = ['aa','bb','cc','dd'] mlist.insert(1,'e') print(mlist) #['aa', 'e', 'bb', 'cc', 'dd']
9) extend 可以加多个值
mlist = ['aa','bb','cc','dd'] mlist1 = ['e','f'] mlist.extend(mlist1) print(mlist) #['aa', 'bb', 'cc', 'dd', 'e', 'f']
10) copy 拷贝赋值给一个新的列表
mlist = ['aa','bb','cc','dd'] mlist1 = mlist.copy() print(mlist1) #['aa', 'bb', 'cc', 'dd']
11) count 统计列表中某个元素的个数
mlist = ['aa','bb','cc','dd'] mlist.append('dd') print(mlist.count('dd')) #2
12) index 打印出指定元素的索引
mlist = ['aa','bb','cc','dd'] print(mlist.index('aa')) #0 print(mlist.index('cc')) #0
13) reverse 反转列表元素
mlist = ['aa','bb','cc','dd'] mlist.reverse() print(mlist) #['dd', 'cc', 'bb', 'aa']
14) sort 排序
mlist = ['aa','cc','cb','bb','dd'] mlist.sort() print(mlist) #['aa', 'bb', 'cb', 'cc', 'dd']
15) clear 清空列表
mlist = ['aa','bb','cc','dd'] mlist.clear() print(mlist) #[]

3. 元组(tuple)

元组是存储在()内的多个值,元组的功能与列表基本上是一样的,与列表的区别是元组是不可变的,可以当作字典的key,主要用来阅读,元组主要操作如下:
0) 元组是不可变类型
t = (1,2,'a','b',3) #本质上 t = tuple((1,2,'a','b',3)) t[0] = 12 #报错 元组中的元素是不可更改的
t=(1,2,[1,2],4,5)
t[2][1]='A'
print(t)                #(1, 2, [1, 'A'])    元组中的元素可以是列表,列表中的元素是可以更改的
1) 切片,按索引取值,顾头不顾尾,这里同字符串和列表
t=(1,2,[1,2],4,5) print(t[1:]) #(2, [1, 2], 4, 5) print(t[2]) #[1, 2] print(t[2][0]) #1
2) len 计算长度
t=(1,2,[1,2],4,5) print(len(t)) #5
3) in/not in 成员计算
t=(1,2,[1,2],4,5) print(1 in t) #True
4) for 循环
for i in t: print(i)

4.字典(dict)

字典存储在{}内,键值对格式,且是无序的,字典是可变类型,键不可变,值可变,字典相关操作如下:
0) 定义字典
info1 = {'name': 'egg','age': 18, 'sex':'male'} info2 = dict(age = 18,sex = 'male',name = 'egg') info3 = dict([('name','egg'),('age',18),('sex','male')]) info4 = {}.fromkeys(['name','age','sex'],None) info5 = {}.fromkeys('hello',None)
1) len 计算长度
info = {'name': 'egg','age': 18, 'sex':'male'} print(len(info)) #3
2) del,pop 删除某个键
#del info = {'name': 'egg','age': 18, 'sex':'male'} del info['name'] print(info) #{'age': 18, 'sex': 'male'} #pop info = {'name': 'egg','age': 18, 'sex':'male'} print(info.pop('name')) #egg print(info.popitem()) #('sex', 'male')
3) keys,values,items 取值
info = {'name': 'egg','age': 18, 'sex':'male'} print(info.keys()) #dict_keys(['name', 'age', 'sex']) print(list(info.keys())[0]) #name print(list(info.values())) #['egg', 18, 'male'] print(list(info.items())) #[('name', 'egg'), ('age', 18), ('sex', 'male')]
4) get,pop 获得某个key对应的value
#get info = {'name': 'egg','age': 18, 'sex':'male'} print(info.get('hh')) #None print(info.get('hh'),'没有这个key') #没有这个key
#pop
print(info.pop('hh','没有这个key'))     #没有这个key
6) updata更新字典,以最新的为最终结果
(1) info = {'name': 'egg','age': 18, 'sex':'male'} info = {'a':1} print(info) #{'a': 1} (2) 重要 info1 = {'name': 'egg', 'age': 18, 'sex': 'male'} info2 = {'age': 18, 'hobbies': 'game'} z = info1.copy() z.update(info2) print(z) #{'name': 'egg', 'age': 18, 'sex': 'male', 'hobbies': 'game'} #合并2个字典,若存在相同键,值以后面一个字典为最终结果

5.集合(set)

集合定义在{}内用逗号分割,每个元素都必须是不可变类型,元素不能重复,若重复,会自动去重,集合相关操作如下:
0) 定义集合
s1 = {1,'a'} s2 = {1,2,3,1,2,4} print(s2) #{1, 2, 3, 4}
1) len 计算长度
s2 = {1,2,3,1,2,4} print(len(s2)) #4
2) in/not in 成员运算
s2 = {1,2,3,1,2,4} print(1 in s2) #True
3) &/intersection 交集
s1 = {'aa','bb','cc','dd'} s2 = {'bb','dd','ee','ff','gg'} print(s1&s2) #{'bb', 'dd'} print(s1.intersection(s2)) #{'bb', 'dd'}
4) union/| 合集
s1 = {'aa','bb','cc','dd'} s2 = {'bb','dd','ee','ff','gg'} print(s1|s2) #{'aa', 'ff', 'gg', 'dd', 'ee', 'bb', 'cc'} print(s1.union(s2)) #{'aa', 'ff', 'gg', 'dd', 'ee', 'bb', 'cc'}
5) difference/- 差集
s1 = {'aa','bb','cc','dd'} s2 = {'bb','dd','ee','ff','gg'} print(s1-s2) #{'cc', 'aa'} 在s1中不在s2中的元素 print(s1.difference(s2)) #{'cc', 'aa'} 在s1中不在s2中的元素 print(s2-s1) #{'ff', 'gg', 'ee'} 在s2中不在s1中的元素 print(s2.difference()) #{'ff', 'gg', 'ee'} 在s2中不在s1中的元素
6) symmetric_difference/^ 对称差集
s1 = {'aa','bb','cc','dd'} s2 = {'bb','dd','ee','ff','gg'} print(s1^s2) #{'cc', 'aa', 'ff', 'ee', 'gg'} print(s1.symmetric_difference(s2)) #{'cc', 'aa', 'ff', 'ee', 'gg'}
7) issuperset 父集、issubset 子集
s1 = {1,2,3} s2 = {1,2} print(s1 >= s2) #True print(s1.issuperset(s2)) #True print(s1 <= s2) #False print(s2.issubset(s1)) #True
8) difference_update 重新赋值
s1 = {1,2,3} s2 = {1,2} s1.difference_update(s2) print(s1) #{3}
9) pop 随机取值
s1 = {1,2,3} print(s1.pop()) #1 默认从开头取
9) add 增加元素
s1 = {1, 2, 3} s1.add(4) print(s1) #{1, 2, 3, 4} s1 = {1, 2, 3} s1.add(1) print(s1) #{1, 2, 3},若集合中已有改元素,则不会增加
10) discard 删除元素
s1 = {1, 2, 3} s1.discard(1) print(s1) #{2, 3} s1 = {1, 2, 3} s1.discard(4) print(s1) #discard 删除某个元素,没有也不会报错,remove是会报错的
11) isdisjoint ,判断2个集合没有共同元素,是返回True
s1 = {1, 2, 3} s2 = {2,3} print(s1.isdisjoint(s2)) #False

END

posted @ 2017-12-08 20:38  丢失固执  阅读(267)  评论(0编辑  收藏  举报