博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
3-day3-list-truple-map.py
阅读量:4964 次
发布时间:2019-06-12

本文共 3348 字,大约阅读时间需要 11 分钟。

 

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 = {
# "河北": {
# "石家庄": ["鹿泉", "藁城", "元氏"],# "邯郸": ["永年", "涉县", "磁县"],# }# "河南": {
# ...# }# "山西": {
# ...# }## }

转载于:https://www.cnblogs.com/cx2c/p/7050098.html

你可能感兴趣的文章
magento根据浏览次数为商品做排行
查看>>
【Win10开发】关于汉堡菜单-SplitView的用法
查看>>
微信小程序—picker(滚动选择器)
查看>>
NAT技术基本原理与应用
查看>>
MySQL 性能优化的最佳20多条经验分享[转]
查看>>
PHP中str_pad()函数的
查看>>
ajax 跨域 headers JavaScript ajax 跨域请求 +设置headers 实践
查看>>
MRC下多个对象的内存管理
查看>>
MMS调试-PDU分析
查看>>
oracle 三表关联查询
查看>>
01背包变形
查看>>
iOS中判断两个圆是否重叠
查看>>
树莓派 Raspberry Pi 与 micro:bit起手式
查看>>
Aspose.Cells 导出指定格式项目(金额、数字、文本)
查看>>
为UIView视图切换添加动画效果
查看>>
微信小程序开发---小程序提供的能力和常用API---5
查看>>
学习mvc3网站
查看>>
ios 开发 谓词详解
查看>>
MySQL 还原
查看>>
maven的pom.xml文件标签含义
查看>>