第一部分day03-元组、字典、字符串
程序员文章站
2023-12-26 13:37:09
1 product_list=[ 2 ['Mac',9000], 3 ['kindle',800], 4 ['tesla',900000], 5 ['python book',105], 6 ['bike',2000], 7 ] 8 pubs_list = [] 9 save = input("pl ......
-----元组-----
元组查询
1 a = (1,2,3,4) 2 print(a[1:2]) #(2,)
购物车练习(列表方法练习)
1 product_list=[ 2 ['mac',9000], 3 ['kindle',800], 4 ['tesla',900000], 5 ['python book',105], 6 ['bike',2000], 7 ] 8 pubs_list = [] 9 save = input("please input money:") 10 if save.isdigit(): 11 save = int(save) 12 while true: 13 14 print("shopping info".center(50,"-")) 15 #、打印商品内容 16 for i,v in enumerate(product_list,1): 17 print(i,v) 18 choice = input("please input nums:") 19 #验证输入是否合法 20 if choice.isdigit(): 21 choice = int(choice) 22 if choice > 0 and choice <= len(product_list): 23 #将用户选择商品用p_iters取出 24 p_iters = product_list[choice-1] 25 # print(p_iters) 26 #如果剩余钱足够,可以继续购买 27 if p_iters[1] < save: 28 pubs_list.append(p_iters) 29 print(p_iters) 30 save -= p_iters[1] 31 else: 32 print("余额不足 %s" % save) 33 elif choice == 'quit': 34 for j in pubs_list: 35 # print(pubs_list) 36 print("您购买的商品 :%s" % j) 37 print("购买商品剩余金额 :%s" % save) 38 break 39 40 else: 41 print("invalid input")
-----字典-----
字典:是python中唯一的映射类型,采用键值对的形式存储数据。
特点:1、字典是无序的,且键(key)可哈希 2、键唯一
不可变类型:整型,字符串,元祖
可变类型:列表,字典
字典的创建
1 a=list() #列表创建 2 print(a) #[] 3 4 dic={'name':'dream'} 5 print(dic) #{'name': 'dream'} 6 7 dic1={} 8 print(dic1) #{} 9 10 dic2=dict((('name','dream'),)) 11 print(dic2) #{'name': 'dream'} 12 13 dic3=dict([['name','dream'],]) 14 print(dic3) #{'name': 'dream'}
id方法使用
1 a = 100 2 print(id(a)) #94845938306592 3 b = a 4 print(id(b)) #94845938306592 5 b = 20 6 print(id(b)) #94457323938848
字典增加
1 dic1 = {'name':'dream'} 2 print(dic1) #{'name': 'dream'} 3 #setdefault,键存在,返回想用的键相应的值;,键不存在,在字典中添加新的键值对 4 ret = dic1.setdefault('age',20) 5 print(dic1) #{'name': 'dream', 'age': 20} 6 print(ret) #20
字典的查询
1 dic2 = {'age': 20, 'name': 'dream'} 2 print(dic2['name']) #dream
显示列表中所有的键
1 print(dic2.keys()) #dict_keys(['age', 'name']) 2 print(list(dic2.keys())) #['name', 'age'] 3 #显示列表中说有的值 4 print(list(dic2.values())) #[20, 'dream'] 5 #显示列表中说有的键、值 6 print(list(dic2.items())) #[('name', 'dream'), ('age', 20)]
字典修改
1 dic3 = {'age': 20, 'name': 'dream'} 2 dic3['name'] = 'rise' 3 print(dic3) #{'name': 'rise', 'age': 20} 4 #update 5 dic4 = {'age':18,'sex':'man'} 6 dic3.update(dic4) 7 print(dic3) #{'age': 18, 'sex': 'man', 'name': 'rise'}
字典删除
1 dic5 = {'age': 18, 'sex': 'man', 'name': 'rise'} 2 3 #del 删除键值对 4 del dic5['age'] 5 print(dic5) #{'sex': 'man', 'name': 'rise'} 6 #clear 清空字典 7 dic5.clear() 8 print(dic5) #{} 9 10 #pop 删除字典中指定键值对,并返回该键值对的值 11 ret = dic5.pop('name') 12 print(ret) #rise 13 print(dic5) #{'sex': 'man', 'age': 18} 14 #popitem 随机删除某组键值对,病以元祖方式返回值 15 ret = dic5.popitem() 16 print(ret) #('sex', 'man') 17 print(dic5) #{'name': 'rise', 'age': 18} 18 #删除整个字典 19 del dic5 20 print(dic5)
字典初始化
1 dic6 = dict.fromkeys(['age', 'sex','name','rise'],'test') 2 print(dic6) #{'rise': 'test', 'sex': 'test', 'age': 'test', 'name': 'test'}
字典嵌套
1 school = { 2 "teachers":{ 3 'xiaowang':["高个子","长的帅"], 4 'xiaohu':["技术好","玩的好"] 5 }, 6 "students":{ 7 "zhangsan":["成绩好","爱讲笑话"] 8 } 9 }
字典嵌套查询
1 print(school['teachers']['xiaohu'][0]) #技术好 2 print(school["students"]["zhangsan"][1]) #爱讲笑话
字典嵌套修改
1 school["students"]["zhangsan"][0] = "眼睛很好看" 2 print(school["students"]["zhangsan"][0]) #眼睛很好看
字典排序
1 dic = {6:'666',2:'222',5:'555'} 2 print(sorted(dic)) #[2, 5, 6] 3 print(sorted(dic.values())) #['222', '555', '666'] 4 print(sorted(dic.items())) #[(2, '222'), (5, '555'), (6, '666')]
循环遍历
1 dic7 = {'name': 'rise', 'age': 18} 2 for i in dic7: 3 print(("%s:%s") % (i,dic7[i])) #name:rise age:18
-----字符串-----
1 a = "this is my progect" 2 #重复输出字符串 3 print(a*2) #重复2次输出 this is my progectthis is my progect 4 #通过索引获取字符串 5 print(a[3:]) #s is my progect 6 #in 方法判度 7 print('is' in a) #true 8 #格式化输出字符串 9 print('%s mode1' % a) #this is my progect mode1 10 11 #字符串拼接 12 a = "this is my progect" 13 b = "test" 14 print("".join([a,b])) #this is my progecttest 15 16 d = "this is my progect" 17 e = "test" 18 f = "" 19 print(f.join([d,e])) #this is my progecttest 20 21 #字符串常用内置方法 22 a = "this is my progect" 23 #居中显示 24 print(a.center(50,'*')) #****************this is my progect**************** 25 #统计 元素在字符串中重复次数 26 print(a.count("is")) #2 27 #首字母大写 28 print(a.capitalize()) #this is my progect 29 #以某个内容结尾字 30 print(a.endswith("ct")) #true 31 #以某个内容开头字 32 print(a.startswith("th")) #true 33 #调整空格数 34 a = "this\t is my progect" 35 print(a.expandtabs(tabsize=10)) #this is my progect 36 #查找一个元素,返回元素索引值 37 a = "this is my progect" 38 print(a.find('is')) #2 39 a = "this is my progect{name},{age}" 40 print(a.format(name='dream',age=18)) #this is my progectdream,18 41 print(a.format_map({'name':'rise','age':20})) #this is my progectrise,20 42 print(a.index('s')) #3 43 #判度字符串时候包含数字 44 print("abc1234".isalnum()) #true 45 #检查是否数字 46 print('12345'.isdigit())#true 47 #检查字符串是否合法 48 print('123abc'.isidentifier()) #false 49 print(a.islower()) #true 判断是否全小写 50 print(a.isupper()) 51 print('f d'.isspace()) #是否包含空格 52 print("my project".istitle()) #首字母大写 true 53 print('my project'.upper()) #my project 54 print('my project'.lower()) #my project 55 print('my project'.swapcase()) #my project 56 print('my project'.ljust(50,"-")) #my project---------------------------------------- 57 print('my project'.rjust(50,'-')) #----------------------------------------my project 58 #去掉字符串空格与换行符 59 print(" my project\n".strip()) #my project 60 print('test') 61 #替换 62 print("my project project".replace('pro','test',1)) #my testject project 63 #从右向左查找 64 print("my project project".rfind('t')) #17 65 #以右为准分开 66 print("my project project".rsplit('j',1)) #['my project pro', 'ect'] 67 print("my project project".title()) #my project project