3-day3-list-truple-map.py |
#! /usr/bin/env python# -*- coding: utf-8 -*-# __author__ = "w.z"# Date: 2017/6/8# 1. 元素分类# 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。# 即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
map1 = {'k1': '', 'k2': ''}
list1 = [11, 22, 33, 44, 55, 66, 66, 77, 88, 99, 90]
list2 = []
list3 = []
for i in list1:
if i < 66:
list2.append(i)
else:
list3.append(i)
map1 = {'k1': list2, 'k2': list3}
print(map1)
# 2. 查找# 查找列表中元素,移除每个元素的空格,并查找以 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"]
li_new = []
for i in li:
i = i.strip()
li_new.append(i)
li = li_new
for i in li:
if i.startswith('a') or i.startswith('A') and i.endswith('c'):
print(i)
tu = ("alec", " aric", "Alex", "Tony", "rain")
tu_list = list(tu)
for i in tu_list:
if i.strip().startswith('a') or i.strip().startswith('A') and i.strip(' ').endswith('c'):
print(i.strip())
dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"}
for i in dic.values():
if i.startswith('a') or i.startswith('A') and i.endswith('c'):
print(i.strip())
# 3. 输出商品列表,用户输入序号,显示用户选中的商品# 商品 li = ["手机", "电脑", '鼠标垫', '游艇']
li = ["手机", "电脑", '鼠标垫', '游艇']
num_goods = {}
for i in range(0, len(li)):
print(i+1, li[i])
num_goods[i+1] = li[i]
num = input('Please input your choice (1-%s): ' % len(li))
mun_1 = int(num) - 1
print(num_goods[int(num)])
# 4 购物车# 功能要求:# 要求用户输入总资产,例如:2000# 显示商品列表,让用户根据序号选择商品,加入购物车# 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。# 附加:可充值、某商品移除购物车# goods = [# {"name": "电脑", "price": 1999},# {"name": "鼠标", "price": 10},# {"name": "游艇", "price": 20},# {"name": "美女", "price": 998},# ]
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
list_store = []
goods_map = {}
goods_price = {}
total = 0
sum1 = input('Please input your money: ')
i = 1
print('There are some goods for you to choice: ')
for items in goods:
print(i, items['name'])
goods_map[i] = items['name'] # 此处生成一个 序列和商品的字典
goods_price[items['name']] = items['price'] #此处生成一个 商品和价格的字典
i += 1
print('商品价格: ', goods_price)
tag = Truewhile tag:
choice = input('Please input your choice num (q quit,d del): ')
if choice != 'q' and choice != 'd':
list_store.append(str(goods_map[int(choice)]))
print(list_store)
elif choice == 'd':
tt = Truewhile tt:
nn = input('购物车清单,请输入要删除的对象(1-n) q退出:')
if nn != 'q':
list_store.pop(int(nn) - 1)
print(list_store)
else:
breakelif choice == 'q':
print(list_store)
breakfor x in list_store:
total += goods_price[x]
if total < int(sum1):
print('商品总价格 %s ,购买成功' % total)
else:
print('购买失败,余额不足')
# 选做题:用户交互,显示省市县三级联动的选择## dic = {# "河北": {# "石家庄": ["鹿泉", "藁城", "元氏"],# "邯郸": ["永年", "涉县", "磁县"],# }# "河南": {# ...# }# "山西": {# ...# }## }