1.车牌区域划分, 现给出以下车牌. 根据车牌的信息, 分析出各省的车牌持有量.
cars = ['鲁A32444', '鲁B12333', '京B8989M', '黑C49678', '黑C46555', '沪B25041', '黑C34567']
locations = {'沪': '上海', '京': '北京', '黑': '黑龙江', '鲁': '山东', '鄂': '湖北', '湘': '湖南'}
结果:
{"上海":1, "北京":1, "黑龙江":3, "山东":2}
cars = ['鲁A32444', '鲁B12333', '鲁B8989M', '黑C49678', '黑C46555', '沪B25041', '黑C34567'] locations = {'沪': '上海', '京': '北京', '黑': '黑龙江', '鲁': '山东', '鄂': '湖北', '湘': '湖南'} result = {} for car in cars: # car就是每个车牌子. '鲁B12333' word = car[0] # 鲁 prov = locations[word] # 山东 # 表示false的东西: 0, "", [], set(), {}, None if result.get(prov): # 湖南 # a += 1 a = a + 1 result[prov] += 1 # 原来有这个省份信息. 数据量+1即可 result['山东'] = result['山东'] + 1 else: result[prov] = 1 # 第一个省份信息 print(result)
2.有如下值li= [11,22,33,44,55,66,77,88,99,90],将所有大于 66 的值保存至字典的第一个key中,将小于
第一种方法:
li = [11,22,33,44,55,66,77,88,99] {'k1': 大于66的所有值列表, 'k2': 小于66的所有值列表} li = [11,22,33,44,55,66,77,88,99] dic = {"k1":[], "k2":[]} for item in li: if item > 66: dic['k1'].append(item) else: dic['k2'].append(item) print(dic)
第二种方法:
li = [11,22,33,44,55,66,77,88,99] dic = {'k1':[77, 88]} for item in li: if item > 66: # setdefault先执行新增流程(判断key存在与否, 存在就不新增, 不存在则新增). 然后会根据key查询出value dic.setdefault("k1", []).append(item) # if dic.get("k1"): # dic['k1'].append(item) # else: # dic['k1'] = [] # dic['k1'].append(item) else: dic.setdefault("k2", []).append(item) # if dic.get("k2"): # dic['k2'].append(item) # else: # dic['k2'] = [] # dic['k2'].append(item) print(dic)
3.数据结构:
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
......
]
shoppingCart = [
{"name": "电脑", "price": 1999, "count":1},
{"name": "美女", "price": 998, "count":3},
]
功能要求:
1、启动程序后,输入用户名密码后,让用户输入余额,
2、然后打印商品列表
页面显示 序号 + 商品名称 + 商品价格,如:
1 电脑 1999
2 鼠标 10
…
n 购物车结算
2、用户输入商品编号或n
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、用户输入n结束购买过程.打印出该用户购买的商品信息, 数量, 单价. 以及余额
思路分析: # 1. xxxx # 2... # n.... # num = input("输入一个你选项:") # if num.isdigit(): # print("xxxxx") # elif num.upper() == "N": # # goods = [ # {"name": "电脑", "price": 1999}, # {"name": "鼠标", "price": 10}, # {"name": "游艇", "price": 20}, # {"name": "美女", "price": 998}, # ] # # shoppingCart = [ # {"name": "电脑", "price": 1999, "count":3}, # ] # # for i in range(len(goods)): # good = goods[i] # print(i+1, good['name'], good['price']) # print("n, 结算") # num = input("输入你要买的商品信息") # if num.isdigit(): # # 买商品了 # num = int(num) # # 拿到你要购买的商品 # pro = goods[num - 1] # pro_name = pro['name'] # for g in shoppingCart: # if pro_name == g['name']: # g['count'] += 1 # break # else: # shoppingCart.append({"name":pro_name, "price":pro['price'], "count": 1}) # # elif num.upper() == "N": # # 结算
goods = [ {"name": "电脑", "price": 1999}, # 1 0 {"name": "鼠标", "price": 10}, # 2 1 {"name": "游艇", "price": 20}, # 3 2 {"name": "美女", "price": 998}, # 4 3 ] # 鼠标 10 shoppingCart = [ ] username = "alex" password = "123456" money = 0 # .... while 1: uname = input("请输入用户名:") upwd = input("请输入密码:") if uname == username and upwd == password: print("登录成功") money += int(input("请输入你都里的钱:")) while 1: # 打印商品信息. 需要用到商品的索引信息 for i in range(len(goods)): good = goods[i] print(i + 1, good['name'], good['price']) print("n 结算购物车") # 让用户选择它想购买的商品 choice = input("请输入您想购买的商品序号或者n:") if choice.upper() == "N": print("购物车结算") for i in range(len(shoppingCart)): product = shoppingCart[i] print(i + 1, product['name'], product['price'], product['count']) print("您的余额是: %s" % money) break else: if choice.isdigit(): choice = int(choice) # 2 index = choice - 1 # 还原商品索引 1 product = goods[index] # 鼠标 10 if money >= product['price']: print("你兜里的钱是够的") # 把商品存到购物车 # 判断购物车内是否购买过该商品 # 两种情况: # 1. 我没买过这种商品. 新增一个商品到购物车 # 2. 我买过这种商品. 如果买过. 把商品数量+1 for item in shoppingCart: # item拿到的是购物车里的每一个商品 if item['name'] == product['name']: # 我找到该商品. 在购物车里面 item['count'] += 1 break # 如果程序能执行到这个break, 意味着 商品我找到了, 如果循环下来. # 没有一个商品执行过这个break else: # 如果能走到else. 证明没有人break过. 没有一个商品和购物车里的商品对的上 shoppingCart.append({"name": product['name'], 'price': product['price'], "count": 1}) # 扣款 money -= product['price'] print("购买成功") else: print("你都里的钱不够. 请重新挑选商品") else: print("对不起, 您输入的内容有误. 请重新输入.") else: print("登录失败") print(money)
4.
1. 菜单: ("查看员工信息","添加员工信息", "修改员工信息", "删除员工信息", "退出")
2. 添加员工信息:
用户输入员工的基本信息(id, name, birthday, salary, input_time),
将员工信息写入到文件emp.db文件内
3. 修改员工信息:
显示所有员工信息. 然后让用户选择要修改的员工的id. 然后让用户输入员工的工资,
将员工的工资修改为用户输入的工资. 其余内容不做改动
4. 删除员工信息:
显示所有员工信息. 然后用户选择要删除的员工id, 根据用户输入的id删除该员工的全部信息
5. 查看员工信息:
显示出所有员工的基本信息.
思路: # f = open("a.txt") # title = f.readline() # title_lst = title.split() # for line in f: # 后面的数据 # data = line.split() # for i in range(len(title_lst)): # title_lst[i] => data[i]
def show_emp_info(): f = open(xxxx) for line in f: print(line) def add_emp_info(): f = open("xxx", mode="a") id = input() name = input() birthday = input() f.write(id, name. xxxxx) def upd_emp_info(): pass def del_emp_info(): pass menu = ("查看员工信息","添加员工信息", "修改员工信息", "删除员工信息", "退出") while 1: for i in range(len(menu)): print(i+1, menu[i]) uinput = input(">>") if uinput == "1": show_emp_info() elif uinput == "2": pass elif uinput == "2": pass elif uinput == "2": pass elif uinput == "2": pass elif uinput == "5": break else: print("啥也不是")