Day 15 python 之 列表、元组、字典
基础:
1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # __author__ = "DaChao" 4 # Date: 2017/6/8 5 6 ''' 7 一:可变不可变 8 可变:列表,字典 9 不可变:数字,字符串,元组 10 11 二:存放值的个数 12 一个值:数字,字符串 13 多个值(容器类型):列表,元组,字典 14 15 三:取值方式 16 直接取值:数字 17 序列类型:字符串,元组,列表 18 映射类型:字典 19 ''' 20 21 # 一、列表 [,,,,] 中括号 22 # a = ['a','b','c','d','c','e'] 23 # b = [5,6,1,2,3] 24 # 25 # b.sort() #对原列表进行排序 26 # print(b) 27 28 # a.remove('c') #移除列表中某个值的第一个匹配项 29 # print(a) 30 31 # print(a.pop()) #移除列表中的一个元素,并且返回该值 32 # print(a) 33 34 # a.insert(1,b) #将对象插入列表 35 # print(a) 36 37 # print(a.index('b')) #从列表中找出某个值第一个匹配项的索引位置 38 39 # a.extend(b) #在列表末尾一次性追加另一个序列中的多个值 40 # print(a) 41 42 # print(a.count('a')) #统计某个元素在列表中的次数 43 44 # a.append("aaa") #在列表末尾添加新的对象 45 # print(a) 46 47 # print(len(a)) #列表长度 48 # print(max(a)) #列表中最大值返回 49 50 # print(a+b) #列表合并相加 51 # print(a*4) #列表复制相乘 52 53 # print(a[0]) 54 # print(a[1:5]) 55 # a[1] = 'bb' #修改列表元素 56 # a.append('dd') #增添列表元素 append 57 # print(a) 58 # del a[0] #删除列表元素 59 # print(a) 60 61 # 二、元祖 () 小括号 62 63 # a = ('a','b','c','d','c','e') 64 # b = (5,6,1,2,3) 65 # 66 # c = a + b 67 # print(c) 68 69 # 三、字典 {key1:valve1,key2:valve2,key3:valve3} 大括号 key是唯一的,但是valve不是唯一的 70 # key键不可变、且唯一,不可用列表表示! 71 72 # dict = {"dachao":"shuai","xiao":"hao","chao":"da"} 73 # 74 # dict["xiao"] = "haoda" 75 # dict["xuan"] = "dada" 76 # del dict["dachao"] 77 # print(dict) 78 79 80 # m = ['a','b',['alex','yuanhao'],'d'] 81 # 82 # for index in range(len(m)): 83 # value = m[index] 84 # if type(value) is list: 85 # for i in range(len(value)): 86 # print(value[i]) 87 # else: 88 # print(value) 89 # for count in range(0,len(m)): 90 # print(m[count]) 91 92 93 94 # m = 'alex123' 95 # *_,a,b = m 96 # print(a,b) 97 # a,*_,b = m 98 # print(a,b) 99 100 # x = 6 #赋值交换 101 # y = 8 102 # print(x,y) 103 # x,y = y,x 104 # print(x,y) 105 106 107 # 一、可变与不可变的定义 108 # id和type不变的前提下,value可变则可变,所以数值和字符串不可变!列表可变 109 110 # x = 300 111 # y = 300 112 # x == y #比较数值 113 # x is y #比较id 114 # type(x) == type(y) #比较类型
作业及练习:
1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # __author__ = "DaChao" 4 # Date: 2017/6/8 5 6 #选做题:用户交互,显示省市县三级联动的选择 7 8 dic1 = {} 9 dic1["东部"] = ("烟台","青岛","威海") 10 dic1["西北部"] = ("德州","东营","淄博") 11 dic = {} 12 dic["山东"] = dic1 13 dic2 = {} 14 dic2["吴国"] = ("周瑜","大乔","小乔") 15 dic2["蜀国"] = ("刘备","关羽","张飞") 16 dic2["魏国"] = ("曹操","司马懿","夏侯惇") 17 dic["三国"] = dic2 18 print(dic) 19 print(dic1) 20 print(dic2) 21 22 23 24 # 四、购物车 25 # 功能要求: 26 # 27 # 要求用户输入总资产,例如:2000 28 # 显示商品列表,让用户根据序号选择商品,加入购物车 29 # 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。 30 # 附加:可充值、某商品移除购物车 31 32 # goods = [ 33 # {"name": "电脑", "price": 1999}, 34 # {"name": "鼠标", "price": 10}, 35 # {"name": "游艇", "price": 20}, 36 # {"name": "美女", "price": 998}, 37 # ] 38 # money = int(input("Please input your all money: ")) 39 # n = 1 40 # goods_shop = [] 41 # for i in goods: #实现商品加序号的显示 42 # print(n,i["name"],i["price"]) 43 # n+=1 44 # 45 # tag = True #实现顾客选择商品,并新建购物车,加入选购商品 46 # while tag: 47 # num = input("Please input your choose number or 'q': ") 48 # if num in ('1','2','3','4'): 49 # goods_shop.append(goods[int(num)-1]) 50 # continue 51 # elif num == 'q': 52 # tag = False 53 # 54 # sum_money = 0 55 # for i in goods_shop: #实现购物车金额汇总 56 # sum_money += int(i['price']) 57 # 58 # tag2 = True #实现购物支付 59 # while tag2: 60 # print("Your total shopping money are : ",sum_money) 61 # buyyn = input("y or n: ") 62 # if buyyn == 'y': 63 # if sum_money <= money: 64 # print("Payment successful!") 65 # else: 66 # print("Sorry,your balance has been insufficient,please promptly value!") 67 # tag2 = False 68 # elif buyyn == "n": 69 # print("Exit!") 70 # tag2 = False 71 72 # 三、输出商品列表,用户输入序号,显示用户选中的商品 73 # 商品 li = ["手机", "电脑", '鼠标垫', '游艇'] 74 75 # li = ["手机", "电脑", '鼠标垫', '游艇'] 76 # for i,v in enumerate(li): 77 # print(i,v) 78 # a = int(input("Please input your choose number: ")) 79 # print(li[a]) 80 81 #二、查找 82 '''查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。''' 83 84 # li = ["alec", " aric", "Alex", "Tony", "rain","AAAAAc"] 85 # for i in range(len(li)): 86 # a = li[i].strip() 87 # if a.startswith('a') or a.startswith('A'): 88 # if a.endswith('c'): 89 # print(a) 90 91 # tu = ("alec", " aric", "Alex", "Tony", "rain","Ac") 92 # for i in range(len(tu)): 93 # a = tu[i].strip() 94 # if a.startswith('a') or a.startswith('A'): 95 # if a.endswith('c'): 96 # print(a) 97 98 # dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"} 99 # for i in dic: # 字典可以直接做循环范围 100 # a = dic[i].strip() 101 # if a.startswith('a') or a.startswith('A'): 102 # if a.endswith('c'): 103 # print(a) 104 105 # print(dic.values()) 106 107 108 109 110 111 112 #一、元素分类 113 114 # 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。 115 # 即: {'k1': 大于66的所有值, 'k2': 小于66的所有值} 116 117 118 # a = [11,22,33,44,55,66,77,88,99,90,100,99] # 119 # b = [] 120 # c = [] 121 # d = ('key1','key2') 122 # 123 # for i in range(0,len(a)): 124 # if a[i] > 66: 125 # b.append(a[i]) 126 # elif a[i] < 66: 127 # c.append(a[i]) 128 # else: 129 # continue 130 # 131 # e = dict.fromkeys(d) #字典的创建 132 # e['key1'] = b 133 # e['key2'] = c 134 # print(e)