1.day 04内容回顾及作业讲解
1 列表:增 append insert extend
2 删 remove pop clear del
3 改 li[索引]='被修改的内容' li[切片]='被修改的内容'
4 查 for循环
5 range
6 元祖:
7 sort(reverse = True)
8 li.sort()
9 print(li.sort())
10 reverse()反转
11 ps:
12 join: list——>str
13 s.join(iterable)
14 li = [1,2,3]
15 s1 = str(li)
16 split:str——>list
2.作业讲解
1 lis = [2,3,'k',['qwe',20,['k',['tt',3,'1']],89],'ab','adv']
2 #1)将列表中的'tt'变成大写(用两种方法)
3 # lis[3][2][1][0]=lis[3][2][1][0].upper()
4 # print(lis)
5 # lis[3][2][1][0]='TT'
6 # print(lis)
7 # 2)将列表中的数字3变成字符串’100’(用两种方式)
8 # lis[1]='100'
9 # lis[3][2][1][1]='100'
10 # print(lis)
11 # lis[1]='100'
12 # lis[3][2][1].remove(3)
13 # lis[3][2][1].insert(1,'100')
14 # print(lis)
15 #3)将列表中的字符串’1’变成数字101(用两种方式)
16 # lis[3][2][1][2]=101
17 # print(lis)
18 # lis[3][2][1].remove('1')
19 # lis[3][2][1].insert(2,101)
20 # print(lis)
21 # lis[3][2][1][2] = int('10'+lis[3][2][1][2])
22 # lis[3][2][1][2] = int(lis[3][2][1][2]) + 100
23 # print(lis)
24 #将3变成33
25 # li = [1,2,3]
26 # li[2] = 33
27 # print(li)
3.dict (字典)相关内容
1 #dict
2 '''
3 #数据类型划分,可变数据类型,不可变数据类型
4 不可变数据类型:元祖 ,bool, int ,str (可哈西)
5 可变数据类型:list ,dict,set (不可哈西)
6 dict key 必须是不可变数据类型,,可哈西
7 value: 任意数据类型
8 dict 优点:二分查找去查找
9 存储大量的关系型数据
10 特点:无序的
11
12 '''
13 # dic= {
14 # 'name':['大梦','小梦'],
15 # 'py9':[{'num':71, 'avg_age':18},
16 # {'num': 71, 'avg_age': 18},
17 # {'num': 71, 'avg_age': 18}],
18 # True:1,
19 # (1,2,3):'wuyiyi',
20 # 2:'二哥'
21 # }
22 # print(dic)
23
24 dic1= {'age':18,'name':'jin','sex':'male',}
(1)增
1 #增:
2 # dic1['high'] = 185#没有键值对,添加
3 # dic1['age'] = 16 #如果有值,则值覆盖
4 # dic1.setdefault('weight')
5 #有键值对,不作任何改变,没有才添加
6 # dic1.setdefault('weight',150)
7 # dic1.setdefault('name','二哥')
8 # print(dic1)
(2)删
# print(dic1.pop('age')) #有返回值,按健去删除
# print(dic1.pop('二哥',None)) #可设置返回值
# print(dic1)
1 #随机删除
2 # dic1.popitem()#
3 # print(dic1.popitem())#有返回值,元组里面删除的是健值
4 # print(dic1)
5
6 # del dic1['name']
7 # print(dic1)
8
9 # del dic1
10 # # print(dic1)
11
12 # 清空字典
13 # dic1.clear()
14 # print(dic1)
(3)改(update)
1 # dic1['age'] = 16
2
3 # dic = {"name":"jin","age":18,"sex":"male"}
4 # dic2 = {"name":"alex","weight":75}
5 # dic2.update(dic) #覆盖添加
6 #
7 # print(dic)
8 # print(dic2)
(4)查
1 #查
2 dic1 = {'age': 18, 'name': 'jin', 'sex': 'male',}
3 # print(dic1.keys(),type(dic1.keys()))#健
4 # print(dic1.values())#值
5 # print(dic1.items())#键值对
6 #打印健
7 # for i in dic1:
8 # print(i)
9 # for i in dic1.keys():
10 # print(i)
11 #打印值
12 # for i in dic1.values():
13 # print(i)
14
15 # a=1
16 # b=2
17 # a,b=b,a
18 # print(a,b)
19 #打印对应元素
20 # a,b=[1,2]
21 # print(a,b)
22
23 # for i in dic1.items():
24 # print(i)
25 # for i,v in dic1.items():
26 # print(i,v)
27
28 # v1 = dic1['name']
29 # print(v1)
30 #
31 #
32 # v2 = dic1['name12']
33 # print(v2)
34 print(dic1.get('name1','没有这个键'))
4. dict嵌套
1 dic = {
2 'name':['alex','wusir','taibai'],
3 'py9':{
4 'time':'1213',
5 'learn_money':19800,
6 'addr':'CBD',
7 },
8 'age':21
9 }
10 # dic['age'] = 56
11 # dic['name'].append('ritian')
12 # print(dic)
13 # dic['name'][1]=dic['name'][1].upper()
14 # print(dic)
15
16 # dic['py9']['female'] = 6
17 # print(dic)
18
19 info = input('>>>') #fhdklah123rfdj12fdjs13
20 for i in info:
21 if i.isalpha():
22 info = info.replace(i,"")
23 l= info.split()
24 print(len(l))
25 # info = input('>>>').strip()
26 # for i in info:
27 # if i.isalpha():
28 # info = info.replace(i," ")
29 # l = info.split()
30 # print(len(l))