Python基本数据类型(练习题)
一、元素分类
有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
# 方法一: a = [11,22,33,44,55,66,77,88,99,90,] l1 = [] l2 = [] for i in a: if i < 66: l1.append(i) elif i > 66: l2.append(i) else: continue temp = {"k1":l1,"k2":l2} print(temp) {'k1': [11, 22, 33, 44, 55], 'k2': [77, 88, 99, 90]}
# 方法二: a = [11,22,33,44,55,66,77,88,99,90,] dic = {"k1":[],"k2":[]} for i in a: if i < 66: dic['k1'].append(i) elif i > 66: dic['k2'].append(i) else: continue print(dic) {'k1': [11, 22, 33, 44, 55], 'k2': [77, 88, 99, 90]}
二、查找
查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。
li = ["alec", " aric", "Alex", "Tony", "rain"]
tu = ("alec", " aric", "Alex", "Tony", "rain")
dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"}
###第一种方式 li = ["alec", " aric", "Alex", "Tony", "rain"] for i in li: new_i = i.strip() ###if判断的顺序,从前到后,或者加括号 if (new_i.startswith('a') or new_i.startswith('A')) and new_i.endswith('c'): print(i) alec aric
###第二种方式
li = ["alec", " aric", "Alex", "Tony", "rain"]
for i in li:
new_i = i.strip()
if new_i.endswith('c'):
if new_i.startswith('a') or new_i.startswith('A'):
print(i)
alec
aric
tu = ("alec", " aric", "Alex", "Tony", "rain")
for i in tu:
new_i = i.strip()
# if判断的顺序,从前到后,或者加括号
if (new_i.startswith('a') or new_i.startswith('A')) and new_i.endswith('c'):
print(i)
alec
aric
dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"} for i in dic.values(): new_i = i.strip() # if判断的顺序,从前到后,或者加括号
if (new_i.startswith('a') or new_i.startswith('A')) and new_i.endswith('c'):
print(i)
aric
三、输出商品列表,用户输入序号,显示用户选中的商品
商品 li = ["手机", "电脑", '鼠标垫', '游艇']
#####方法一######### li = ["手机", "电脑", '鼠标垫', '游艇'] for i,j in enumerate(li): print(i+1,j) num = input('num:') #索引 num = int(num) good = li[num-1] print(good) 1 手机 2 电脑 3 鼠标垫 4 游艇 num:1 手机
#######方法二####### li = ["手机", "电脑", '鼠标垫', '游艇'] for i,j in enumerate(li,1): print(i,j) num = input('num:') #索引 num = int(num) len_li = len(li) if num > 0 and num <= len_li: good = li[num-1] print(good) else: print("商品不存在!")
四、购物车
功能要求:
- 要求用户输入总资产,例如:2000
- 显示商品列表,让用户根据序号选择商品,加入购物车
- 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
- 附加:可充值、某商品移除购物车
goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ]
##方式一: asset_all = 0 car_dict = { "电脑":{'price':"单个商品价格","num":"购买数量"} } cat_list = [] i1 = input("请输入总资产:") asset_all = int(i1) goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ] for i in goods: #i,每一个列表的元素,字典 print(i['name'],i['price']) while True: i2 = input('请选择商品(Y/y结算)):') if i2.lower() == "y": break for j in goods: if j['name'] == i2: #j = {"name":"电脑",”price”: 1999}, #print(j) cat_list.append(j) #结算 # print(cat_list) all_price = 0 for item in cat_list: p = item['price'] all_price = all_price + p print(asset_all,all_price) if all_price > asset_all: print('超出预算') else: print("购买成功") 输出内容: 请输入总资产:4000 电脑 1999 鼠标 10 游艇 20 美女 998 请选择商品(Y/y结算)):电脑 请选择商品(Y/y结算)):鼠标 请选择商品(Y/y结算)):y 4000 2009 购买成功
#方式二: asset_all = 0 i1 = input("请输入总资产:") asset_all = int(i1) goods = [ {"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ] for i in goods: # {"name": "电脑", "price": 1999} print(i['name'],i['price']) car_dict = {} # car_dict = { # "电脑":{'price':单价, num:123} # } while True: i2 = input("请选择商品(Y/y结算):") if i2.lower() == "y": break #循环所有的商品,找到需要的商品 for item in goods: if item['name'] == i2: # item = {"name": "电脑", "price": 1999} name = item['name'] # 判断购物车是否已经有商品,有,num + 1 if name in car_dict.keys(): #pass car_dict[name]['num'] = cat_dict[name]['num'] + 1 else: car_dict[name] = {"num":1,'single_price':item['price']} all_price = 0 for k,v in car_dict.items(): n = v['single_price'] m = v['num'] all_sum = m * n all_price = all_price + all_sum if all_price > asset_all: print("预算不足") else: print("购买成功")
输出内容: 请输入总资产:2000 电脑 1999 鼠标 10 游艇 20 美女 998 请选择商品(Y/y结算):电脑 请选择商品(Y/y结算):游艇 请选择商品(Y/y结算):y 预算不足
五、用户交互,显示省市县三级联动的选择
dic = { "河北": { "石家庄": ["鹿泉", "藁城", "元氏"], "邯郸": ["永年", "涉县", "磁县"], } "河南": { ... } "山西": { ... } }
dic = { "河北":{ "石家庄":["鹿泉","藁城","元氏"], "邯郸":["永年","涉县","磁县"], }, "河南":{ "郑州":["金水区","惠济区","上街区"], "开封":["龙亭区","鼓楼区","祥符区"], }, "山西":{ "大同":["矿区","南郊区","大同县"], "临汾":["尧都区","襄汾县","浮山县"], }, } # 循环输出所有的省 for i in dic: print(i) i1 = input("请输入省份:") a = dic[i1] for j in a: print(j) ###循环输出所有的市 i2 = input("请输入市:") b = dic[i1][i2] for z in b: print(z)
输出内容; 河北 河南 山西 请输入省份:河北 邯郸 石家庄 请输入市:石家庄 鹿泉 藁城 元氏