python学习的第二天
今天一天的知识点很多,详细介绍了python 字符串,列表,元祖,字典,集合等的方法。抛砖引玉... name = 'alex' ###那么name就是str类的对象
1.字符串
1.1 capitalize
字符串首字母大写,自身不变,会生成一个新的值。
1 #/usr/bin/python 2 3 new_name = name.capitalize() 4 print(new_name) 5 print(name)
1.2 casefold
将所有大写变小写,包括德语等等...
1 #/usr/bin/python 2 name = 'Alex' 3 name_new = name.casefold() 4 print(name_new) 5 print(name)
1.3 lower
将所有大小写变小写
1 #/usr/bin/python 2 name = 'ALEx' 3 new_name = name.lower() 4 print(name) 5 print(new_name)
1.4 center
文本居中,参数1表示总长度,参数2表示空白处填充的字符(长度为1)
#/usr/bin/python name = 'alex' new_name = name.center(20,'*') print(new_name)
1.5 count
表示传入值在字符串中出现的次数
参数1:要查找的值(子序列)
参数2:起始位置(索引)
参数3: 结束位置(索引)
#/usr/bin/python name = 'alexbsefsdfwefsadsf' v = name.count(a) d = name.count(1,7) print(v) print(d)
1.6 startswith endswith
是否已xx开头,是否已xx结束
#/usr/bin/python name = 'alex' print(name.startswith('al')) print(name.startswith('ex'))
1.7 expandtabs
找到制表符 ,进行替换,包含前面的值
1 #/usr/bin/python 2 name = 'alex/tbb/tcc/tkkk' 3 v = name.expandtabs(20) 4 print(v)
1.8 find index
找到指定子序列的索引位置,不存在返回-1
1 #/usr/bin/python 2 name = 'alex' 3 v = name.find('0') 4 print(v) 5 v = name.index('0') 6 print(v)
1.9 字符串输出格式化
#/usr/bin/python tpl = "我是:%s;年龄:%s;性别:%s" tpl = "我是:{0};年龄:{1};性别:{2}" v = tpl.format("李杰",19,'都行') print(v) tpl = "我是:{name};年龄:{age};性别:{gender}" v = tpl.format(name='李杰',age=19,gender='随意') print(v) tpl = "我是:{name};年龄:{age};性别:{gender}" v = tpl.format_map({'name':"李杰",'age':19,'gender':'中'}) print(v)
1.10 是否是数字
1 #/usr/bin/python 2 num = '②' 3 v1 = num.isdecimal() # '123' 4 v2 = num.isdigit() # '123','②' 5 v3 = num.isnumeric() # '123','二','②' 6 print(v1,v2,v3)
1.11 是否是表示符
1 #/usr/bin/python 2 n = 'name' 3 v = n.isidentifier() 4 print(v)
1.12 是否全部是大小写
1 #/usr/bin/python 2 name = "ALEX" 3 v = name.islower() 4 print(v) 5 v = name.isupper() 6 print(v)
1.13 全部变大写
#/usr/bin/python name = 'alex' v = name.upper() # lower() print(v)
1.14 是否包含隐藏的xx
#/usr/bin/python name = "钓鱼要钓刀鱼, 刀鱼要到岛上钓" v = name.isprintable() print(v)
1.15 是否全部是空格
1 #/usr/bin/python 2 name = ' ' 3 v = name.isspace() 4 print(v)
1.16 元素拼接
1 #/usr/bin/python 2 3 name = 'alex' 4 5 v = "_".join(name) # 内部循环每个元素 6 print(v) 7 8 name_list = ['海峰','杠娘','李杰','李泉'] 9 v = "搞".join(name_list) 10 print(v)
1.17 左右填充数据(ljust,rjust)
1 #/usr/bin/python 2 name = 'alex' 3 v = name.rjust(20,'*') 4 print(v)
1.18 maketrans + translate
1 #/usr/bin/python 2 # m = str.maketrans('aeiou','12345') # 对应关系 3 # 4 # name = "akpsojfasdufasdlkfj8ausdfakjsdfl;kjer09asdf" 5 # v = name.translate(m) 6 # print(v)
1.19 partition
分割,元素保留
1 #/usr/bin/python 2 # content = "李泉SB刘康SB刘一" 3 # v = content.partition('SB') # partition 4 # print(v)
1.20 replace 替换
1 #/usr/bin/python 2 # content = "李泉SB刘康SB刘浩SB刘一" 3 # v = content.replace('SB','Love') 4 # print(v) 5 # v = content.replace('SB','Love',1) 6 # print(v)
1.21 strip
移除空白,自定义
1 #/usr/bin/python 2 # name = 'alex ' 3 # v = name.strip() # 空白, , 4 # print(v)
1.22 swapcase
大小写转化
1 #/usr/bin/python 2 # name = "Alex" 3 # v = name.swapcase() 4 # print(v)
1.23 zfill
填充0
1 #/usr/bin/python 2 # name = "alex" 3 # v = name.zfill(20) 4 # print(v) 5 6 # v1 = 'alex' 7 # v2 = 'eric' 8 # 9 # v = v1 + v2 # 执行v1的__add__功能 10 # print(v)
1.24 常用字符串功能总结和额外功能
1 #/usr/bin/python 2 常用功能: 3 # name = 'alex' 4 # name.upper() 5 # name.lower() 6 # name.split() 7 # name.find() 8 # name.strip() 9 # name.startswith() 10 # name.format() 11 # name.replace() 12 # "alex".join(["aa",'bb']) 13 14 15 额外功能: 16 # name = "alex" 17 # name[0] 18 # name[0:3] 19 # name[0:3:2] 20 # len(name) 21 # for循环,每个元素是字符
2 int 整数
2.1 bit_length
当前整数的二进制表示,最少位数
1 #/usr/bin/python
2 # age = 4 # 100
3 # print(age.bit_length())
2.2 to_bytes
获取当前数据的字节表示
1 #/usr/bin/python 2 # age = 15 3 # v = age.to_bytes(10,byteorder='big') 4 # v = age.to_bytes(10,byteorder='little') 5 # print(v) 6 # 00000000 00001111 -> 15
3 bool 布尔值
v = 0 # 1,-1
v = ""
v = []
--> 空内容:False
4 list 列表
list 列表是可变类型
4.1 append
追加
1 #/usr/bin/python 2 user_list = ['李白','刘禹锡','刘一番','杜甫,'小小萝莉'] # 可变类型 3 user_list.append('刘章') 4 print(user_list)
4.2 clear
清空
#/usr/bin/python user_list = ['李白','刘禹锡','刘一番','杜甫,'小小萝莉'] # 可变类型 user_list.clear() print(user_list)
4.3 copy
拷贝,浅拷贝
1 #/usr/bin/python 2 user_list = ['李白','刘禹锡','刘一番','杜甫,'小小萝莉'] # 可变类型 3 v = user_list.copy() 4 print(user_list) 5 print(v)
4.3 count
计数
#/usr/bin/python user_list = ['李白','刘禹锡','刘一番','杜甫,'小小萝莉'] # 可变类型 v= user_list.count('李白') print(v) print(user_list)
4.4 extend
扩展原列表
#/usr/bin/python user_list = ['李白','刘禹锡','刘一番','杜甫,'小小萝莉'] # 可变类型 user_list.extent('李白酒','杜康') print(user_list)
4.5 index
查找元素索引,没有就报错
#/usr/bin/python user_list = ['李白','刘禹锡','刘一番','杜甫,'小小萝莉'] # 可变类型 v= user_list.index('李白') print(v) print(user_list)
4.6 pop
获取并且删除元素,根据索引
1 #/usr/bin/python 2 user_list = ['李白','刘禹锡','刘一番','杜甫,'小小萝莉'] # 可变类型 3 v= user_list.pop(3) 4 print(v) 5 print(user_list)
4.7 remove
删除元素,根据值
#/usr/bin/python user_list = ['李白','刘禹锡','刘一番','杜甫,'小小萝莉'] # 可变类型 v= user_list.remove('李白') print(v) print(user_list)
4.8 reverse
翻转
1 #/usr/bin/python 2 user_list = ['李白','刘禹锡','刘一番','杜甫,'小小萝莉'] # 可变类型 3 user_list.reverse() 4 print(user_list)
4.9 sort
1 #/usr/bin/python 2 # nums = [11,22,3,3,9,88] 3 # print(nums) 4 # 排序,从小到大 5 # nums.sort() 6 # print(nums) 7 # 从大到小 8 # nums.sort(reverse=True) 9 # print(nums)
4.10 额外功能
1 #/usr/bin/python 2 # user_list = ['李泉','刘一','李泉','刘康','豆豆','小龙'] 3 # user_list[0] 4 # user_list[1:5:2] 5 # del user_list[3] 6 # for i in user_list: 7 # print(i) 8 # user_list[1] = '姜日天' 9 # user_list = ['李泉','刘一','李泉','刘康','豆豆',['日天','日地','泰迪'],'小龙'] 10 11 12 # li = ['eric','alex','tony'] 13 # 14 # v = len(li) 15 # print(v) 16 # 17 # li.append('seven') 18 # print(li) 19 # 20 # li.insert(0,'Tony') 21 # print(li) 22 # 23 # li[1] = 'Kelly' 24 # 25 # li.remove('eric') 26 # print(list) 27 # 28 # v = li.pop(1) 29 # print(v) 30 # print(li) 31 # 32 # del li[2] 33 34 35 # del li[0:2] # 0 =<x < 2 36 # print(li) 37 38 # li.reverse() 39 # print(li) 40 41 # for i in li: 42 # print(i)
5 range enumerate
1 #/usr/bin/python 2 # 1. 请输出1-10 3 # 2.7: 立即生成所有数字 4 # range(1,11) # 生成 1,23,,4,56.10 5 6 # 3.x: 不会立即生成,只有循环迭代时,才一个一个生成 7 # for i in range(1,11): # 8 # print(i) 9 10 # for i in range(1,11,2): # 11 # print(i) 12 13 # for i in range(10,0,-1): # 14 # print(i) 15 16 # 1. 3.x 不会立生成,迭代之后才一个一个创建; 17 """ 18 - 2.7: 19 range() 20 xrange() 不会立生成,迭代之后才一个一个创建; 21 - 3.x 22 range() 不会立生成,迭代之后才一个一个创建; 23 """ 24 # 2. range: 三个参数 25 # 26 # li = ['eric','alex','tony'] 27 # # range,len,li循环 28 # for i in range(0,len(li)): 29 # ele = li[i] 30 # print(ele) 31 32 33 # li = ['eric','alex','tony'] 34 # for i in li: 35 # print(i) 36 37 # for i in range(0,len(li)): 38 # print(i+1,li[i]) 39 40 41 # enumerate额外生成一列有序的数字 42 # li = ['eric','alex','tony'] 43 # for i,ele in enumerate(li,1): 44 # print(i,ele) 45 # 46 # v = input('请输入商品序号:') 47 # v = int(v) 48 # item = li[v-1] 49 # print(item)
6 tuple 元祖
不可被改变的列表,不可变类型
6.1 count
获取个数
1 #/usr/bin/python 2 # user_tuple = ('alex','eric','seven','alex') 3 # v = user_tuple.count('alex') 4 # print(v)
6.2 index
获取值得第一个索引
1 #/usr/bin/python 2 # user_tuple = ('alex','eric','seven','alex') 3 # v = user_tuple.index('alex') 4 # print(v)
6.3 额外功能
1 #/usr/bin/python 2 # user_tuple = ('alex','eric','seven','alex') 3 # for i in user_tuple: 4 # print(i) 5 6 # v = user_tuple[0] 7 8 # v = user_tuple[0:2] 9 # print(v) 10 11 # user_tuple = ('alex','eric','seven',['陈涛','刘浩','赵芬芬'],'alex') 12 # user_tuple[0] = 123 x 13 # user_tuple[3] = [11,22,33] x 14 # user_tuple[3][1] = '刘一' 15 # print(user_tuple) 16 17 # li = ['陈涛','刘浩',('alex','eric','seven'),'赵芬芬'] 18 # ****** 元组最后,加逗号 ****** 19 # li = ('alex',) 20 # print(li)
7 dict 字典
可变类型
7.1 clear
清空
1 #/usr/bin/python 2 # dic = {'k1':'v1','k2':'v2'} 3 # dic.clear() 4 # print(dic)
7.2 copy
浅拷贝
1 #/usr/bin/python 2 # dic = {'k1':'v1','k2':'v2'} 3 # v = dic.copy() 4 # print(v)
7.3 get
获取key数据,不存在不报错
1 #/usr/bin/python 2 # dic = {'k1':'v1','k2':'v2'} 3 # v = dic.get('k1111',1111) 4 # print(v) 5 # v = dic['k1111'] 6 # print(v)
7.4 pop
删除并获取对应的value值
1 #/usr/bin/python 2 # dic = {'k1':'v1','k2':'v2'} 3 # v = dic.pop('k1') 4 # print(dic) 5 # print(v)
7.5 popitem
随机删除键值对,并获取到删除的键值对
1 #/usr/bin/python 2 # dic = {'k1':'v1','k2':'v2'} 3 # v = dic.popitem() 4 # print(dic) 5 # print(v) 6 7 # k,v = dic.popitem() # ('k2', 'v2') 8 # print(dic) 9 # print(k,v) 10 11 # v = dic.popitem() # ('k2', 'v2') 12 # print(dic) 13 # print(v[0],v[1])
7.6 setdefault
增加,如果存在不做任何操作
1 #/usr/bin/python 2 # dic = {'k1':'v1','k2':'v2'} 3 # dic.setdefault('k3','v3') 4 # print(dic) 5 # dic.setdefault('k1','1111111') 6 # print(dic)
7.7 update
批量增加或修改
1 #/usr/bin/python 2 # dic = {'k1':'v1','k2':'v2'} 3 # dic.update({'k3':'v3','k1':'v24'}) 4 # print(dic)
7.8 fromkeys
生成字典
1 #/usr/bin/python 2 3 # dic = dict.fromkeys(['k1','k2','k3'],123) 4 # print(dic) 5 # dic = dict.fromkeys(['k1','k2','k3'],123) 6 # dic['k1'] = 'asdfjasldkf' 7 # print(dic) 8 9 # dic = dict.fromkeys(['k1','k2','k3'],[1,]) 10 # { 11 # k1: 123123213, # [1,2] 12 # k2: 123123213, # [1,] 13 # k3: 123123213, # [1,] 14 # } 15 # dic['k1'].append(222) 16 # print(dic)
7.9 额外功能
1 #/usr/bin/python 2 # - 字典可以嵌套 3 # - 字典key: 必须是不可变类型 4 # dic = { 5 # 'k1': 'v1', 6 # 'k2': [1,2,3,], 7 # (1,2): 'lllll', 8 # 1: 'fffffffff', 9 # 111: 'asdf', 10 # } 11 # print(dic) 12 # key: 13 # - 不可变 14 # - True,1 15 16 # dic = {'k1':'v1'} 17 # del dic['k1'] 18 19 # 布尔值: 20 # 1 True 21 # 0 False 22 # 23 # bool(1111)
8 set 集合
不可重复的列表,可变类型
8.1 difference
集合1中存在 集合2中不存在或者是集合2中存在 集合1中不存在
1 #/usr/bin/python 2 # s1 = {"alex",'eric','tony','李泉','李泉11'} 3 # s2 = {"alex",'eric','tony','刘一'} 4 # v = s1.difference(s2) 5 # print(v)
8.2 difference_update
集合1中存在,集合2中不存在,然后对集合1清空,然后再重新复制。
集合2中存在,集合1中不存在,然后对集合2清空,然后再重新复制
1 #/usr/bin/python 2 # s1 = {"alex",'eric','tony','李泉','李泉11'} 3 # s2 = {"alex",'eric','tony','刘一'} 4 # s1.difference_update(s2) 5 # print(s1)
8.3 symmetric_difference
1 #/usr/bin/python 2 # s1 = {"alex",'eric','tony','李泉','李泉11'} 3 # s2 = {"alex",'eric','tony','刘一'} 4 # v = s1.symmetric_difference(s2) 5 # print(v)
8.4 交集
1 #/usr/bin/python 2 # s1 = {"alex",'eric','tony','李泉','李泉11'} 3 # s2 = {"alex",'eric','tony','刘一'} 4 # v = s1.intersection(s2) 5 # print(v)
8.5 union
并集
1 #/usr/bin/python 2 # s1 = {"alex",'eric','tony','李泉','李泉11'} 3 # s2 = {"alex",'eric','tony','刘一'} 4 # v = s1.union(s2) 5 # print(v)
8.6 discard
移除
1 #/usr/bin/python 2 # s1 = {"alex",'eric','tony','李泉','李泉11'} 3 # s1.discard('alex') 4 # print(s1)
8.7 update
1 #/usr/bin/python 2 # s1 = {"alex",'eric','tony','李泉','李泉11'} 3 # s1.update({'alex','123123','fff'}) 4 # print(s1)
8.8 额外功能
1 #/usr/bin/python 2 # s1 = {"alex",'eric','tony','李泉','李泉11'} 3 # for i in s1: 4 # print(i) 5 6 # s1 = {"alex",'eric','tony','李泉','李泉11',(11,22,33)} 7 # for i in s1: 8 # print(i)