1.列表增删改查,
1 import copy 2 3 names=["zhangyang","guyun","xiangpeng",["alex","jack"],"chenronghua","xuliangchen"] 4 print(names[::2])#上下两句一样 5 print(names[0:-1:2]) 6 for i in names: 7 print(i) 8 9 10 ‘‘‘ 11 name2=copy.deepcopy(names)#深copy,第二层都copy 12 # name2=names.copy()#浅浅的copy,第一场复制,第二层浅浅复制,改了也会改,相当于第二层存了地址 13 print(names,name2) 14 names[2]="向鹏" 15 names[3][0]="ALEX" 16 print(names) 17 print(name2) 18 ‘‘‘ 19 20 21 ‘‘‘ 22 # 增 23 names.append("leihaidong")#插入数据 24 names.insert(1,"chenronghua")#选择位置插入数据,不能批量插入 25 names.insert(3,"xinzhiyu") 26 27 # 改 28 names[2]="xiedi" 29 30 31 # 查 32 # print(names[0],names[2]) 33 # print(names[1:3])#[]中包括开始,不包括结尾,顾头不顾尾,切片 34 # print(names[-1]) 35 # print(names[-3:-1])#遵循从左往右取数, 36 # print(names[-3:])#防止顾前不顾尾操作,后边不写,这样能取到最后一个数,前边不写,可以直接从第一个取 37 38 # 删除 39 # names.remove("chenronghua") 40 # del names[1] 41 # names.pop(1)#默认不输出删除最后一个,输入的话删除相应的数据 42 43 print(names) 44 # print(names.index("xiedi"))#返回xiedi的位置 45 # print(names[names.index("xiedi")]) 46 # print(names.count("chenronghua")) 47 # names.clear()#清空列表 48 # names.reverse()#反转列表 49 # names.sort()#排序特殊字符,数组,大写字母,小写字母,按ascii中的顺序 50 names2=[1,2,3,4] 51 52 names.extend(names2) 53 print(names,names2)‘‘‘
2.三级菜单,并修改其中的一个数据
1 av_catalog={ 2 "欧美":{ 3 "www.youporn.com":["很多免费的,世界最大的","质量一般"], 4 "www.pronhub.com":["很多免费的,也很大","质量比youporn高点"], 5 "letmedothitoyou.com":["多是自拍,高质量图片很多","资源不多,更新慢"], 6 "x-art.com":["质量很高,真的很高","全部收费,屌丝请绕过"] 7 }, 8 "日韩":{ 9 "tokyo-hot":["质量怎样不清楚,个人已经不喜欢日韩范了","听说是收费的"] 10 }, 11 "大陆":{ 12 "1024":["全部免费,真好,好人一生平安","服务器在国外,慢"] 13 } 14 } 15 16 av_catalog["大陆"]["1024"][1]="可以在国内做镜像" 17 18 av_catalog.setdefault("taiwan",{"www.baidu.com":[1,2]})#先去字典里查找key,如果有直接返回,如果没有,创建赋值后边的值 19 20 print(av_catalog) 21 print(av_catalog.keys())#打印key 22 print(av_catalog.values())#打印values
3.浅赋值
1 import copy 2 person1=[‘name‘,[‘saving‘,100]] 3 ‘‘‘ 4 # 浅copy,三种浅copy 5 p1=copy.copy(person1) 6 p2=person1[:] 7 p3=list(person1) 8 ‘‘‘ 9 10 p1=person1[:] 11 p2=person1[:] 12 p1[0]=‘alex‘ 13 p2[0]=‘fengjie‘ 14 15 p1[1][1]=50 16 17 print(p1) 18 print(p2)
4.购物车
1 # 元组,用()标识,只有count和index,一般存不可更改的数据 2 ‘‘‘ 3 names=(‘alex‘,‘jack‘) 4 print(names.count(‘alex‘)) 5 print(names.index(‘alex‘)) 6 ‘‘‘ 7 8 product_list=[ 9 (‘Iphone‘,5800), 10 (‘Mac Pro‘,9800), 11 (‘Bike‘,800), 12 (‘Watch‘,10600), 13 (‘Alex Python‘,120), 14 ] 15 shopping=[] 16 salary=input("Input your salary:") 17 if salary.isdigit(): 18 salary=int(salary) 19 while True: 20 for index,item in enumerate(product_list):#enumerate取出下标 21 print(index,item) 22 # for item in product_list: 23 # print(product_list.index(item),item)获得index作为商品列表,上边也行 24 user_choice=input("选择要买嘛?>>>:") 25 if user_choice.isdigit(): 26 user_choice=int(user_choice) 27 if user_choice<len(product_list) and user_choice>=0: 28 p_item=product_list[user_choice] 29 if p_item[1]<=salary:#买的起 30 shopping.append(p_item) 31 salary-=p_item[1] 32 print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m"%(p_item,salary))#红色31,绿色32 33 else: 34 print("\033[41;1m你的余额只剩[%s]啦,还买毛线\033[0m" % salary) 35 else: 36 print("product code [%s] is not exits!"% user_choice) 37 38 elif user_choice==‘q‘: 39 print("---------shopping list---------") 40 for p in shopping: 41 print(p) 42 print("you current balance:",salary) 43 exit() 44 else: 45 print("invalid option")