7.3 使用while 循环来处理列表和字典
# 7.3.1 在列表之间移动元素
# confirmed_users.py
# 首先,创建一个待验证用户列表
# 和一个用于存储已验证用户的空列表
uncomfirmed_users = ['james','star','wendy']
comfirmed_users = []
# 验证每个用户,直到没有未验证用户为止
# 将每个经过验证的列表都移到已验证用户列表中
while uncomfirmed_users:
current_user = uncomfirmed_users.pop() # pop.() 默认移除列表中最后一个元素
print("Verifying use: " + current_user.title())
comfirmed_users.append(current_user) # 空列表已经在这段码块中更新
# 显示所有已验证的用户
print("\nThe following users have been comfirmed:")
for comfirmed_user in comfirmed_users:
print(comfirmed_user.title())
# 7.3.2 删除包含特定值的所有列表元素
#pets.py
pets = ['dog','cat','goldfish','cat','rabbit','tigger','cat','chick']
# 循环寻找列表中的'cat',并且移除所有'cat',直到列表中没有这条信息
while 'cat' in pets:
pets.remove('cat')
print(pets)
# 7.3.3 使用用户输入来填充字典
# mountain_poll.py
responses = {}
polling_active = True
while polling_active:
# input()函数,提示用户需要输入信息
name = input("What's your name?")
response = input("Which mountain would you like to climb someday?")
# 字典 {'keys','values'} 等效于 keys = values,name = response
responses[name] = response
repeat = input("would you like to let another person repond? (yes/no)")
if repeat == 'no':
polling_active = False
print("\n---Poll Result---")
for name, response in responses.items():
print(name.title() + " would like to climb " + response + ".")
# 7-8 熟食店
sandwich_orders = ['tomato','potato','vegetable']
finished_sandwiches = []
while sandwich_orders:
current_sandwich = sandwich_orders.pop()
print(current_sandwich + ", I made your tuna sandwich.")
finished_sandwiches.append(current_sandwich)
print("All the sandwiches are finished.")
for finished_sandwich in finished_sandwiches:
print(finished_sandwich.title())
# 7-9 五香烟熏牛肉
sandwich_orders = ['tomato','pastrami','potato','pastrami','vegetable','pastrami']
finished_sandwiches = []
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
print("Pastrami sold out!")
while sandwich_orders:
current_sandwich = sandwich_orders.pop()
print(current_sandwich + ", I made your tuna sandwich.")
finished_sandwiches.append(current_sandwich)
print("All the sandwiches are finished.")
for finished_sandwich in finished_sandwiches:
print(finished_sandwich.title())
# 7-10 梦想的度假胜地
responses = {}
polling_active = True
while polling_active:
name = input("What's your name?")
response = input("If you could visit one place in the world," +
" where would you go?")
responses[name] = response
repeat = input("Would you like to let another person respond? (yes/no) ")
if repeat == 'no':
polling_active = False
print("---Poll Result---")
for name, response in responses.items():
print(name.title() + " would like to visit " + response + ".")
7.3 使用while 循环来处理列表和字典的更多相关文章
- 使用while循环来处理列表和字典——参考Python编程从入门到实践
1. 在列表之间移动元素 unconfirmed_users = ['alice', 'brian', 'candace'] confirmed_users = [] # 验证每个用户,知道没有未验证 ...
- input函数以及while处理列表和字典
一.函数input()的工作原理 .input()函数:获取输入的字符串 示例: message = input('请输入信息,方便电脑显示') print(message) print('您输入的信 ...
- python基础之循环结构以及列表
python基础之编译器选择,循环结构,列表 本节内容 python IDE的选择 字符串的格式化输出 数据类型 循环结构 列表 简单购物车的编写 1.python IDE的选择 IDE的全称叫做集成 ...
- Bash实用技巧:同时循环两个列表
摘要: 你会学到一种原创的同时循环两个列表的方法.类似于Python或者Haskell的zip函数,非常简洁直观,效果如下: $ paste <( ) <( ) | while read ...
- 第五篇:python基础之循环结构以及列表
python基础之循环结构以及列表 python基础之编译器选择,循环结构,列表 本节内容 python IDE的选择 字符串的格式化输出 数据类型 循环结构 列表 简单购物车的编写 1.pyth ...
- vue.js循环for(列表渲染)详解
vue.js循环for(列表渲染)详解 一.总结 一句话总结: v-for <ul id="example-1"> <li v-for="item in ...
- Python基础、判断、循环、列表、字典,day1
一.Python 简介 1.介绍 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标 ...
- join,列表和字典用for循环的删除,集合,深浅拷贝
1.join() 将列表转换成字符串,并且每个字符之间用另一个字符连接起来,join后面必须是可迭代的对象(字符串,列表,元组,字典,集合),数字不能迭代 例如: s = ['a','b','c'] ...
- python中for循环里去修改列表注意的事项
你的微信好友当中有 5 个推销的,他们存在一个列表 # black_list=['卖茶叶', '卖面膜', '卖保险', '卖花生', '卖手机'] # 当中, 请把这 5 个人分别从 black_l ...
随机推荐
- git下载安装、配置及idea初始化
安装 wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.19.0.tar.gz git 安装依赖 yum -y insta ...
- SQL计算字符串里的子字符串出现个数
在某个页面,需要显示每条记录中有几个图片文件.图片文件名列表存储在mysql表里的photo_files字段,文件名之间用一个空格分开.类似'images\rpt201503121.jpg image ...
- 解析underscore中的throttle
什么是throttle(节流) Throttling enforces a maximum number of times a function can be called over time. 简单 ...
- Maven的原理和使用
一.Maven能做什么 1.假设我们有10个项目,都需要引入spring core模块,那么需要十份重复的Spring Core.jar和commons-logging.jar 使用Maven:mav ...
- UVA - 1600 Patrol Robot (巡逻机器人)(bfs)
题意:从(1,1)走到(m,n),最多能连续穿越k个障碍,求最短路. 分析:obstacle队列记录当前点所穿越的障碍数,如果小于k可继续穿越障碍,否则不能,bfs即可. #pragma commen ...
- Java8系列 (四) 静态方法和默认方法(转载)
静态方法和默认方法 我们可以在 Comparator 接口的源码中, 看到大量类似下面这样的方法声明 //default关键字修饰的默认方法 default Comparator<T> t ...
- Codeforces 460C 二分结果+线段树维护
发现最近碰到好多次二分结果的题目,上次多校也是,被我很机智的快速过了,这个思想确实非常不错.在正面求比较难处理的时候,二分结果再判断是否有效往往柳暗花明. 这个题目给定n个数字的序列,可以操作m次,每 ...
- eclipse 项目启动不了问题
有可能是因为dubugger 打多了,所有启动不起来 解决方案:window——show view ——other....——breakpoints去除所有断点
- MySQL5.7忘记密码解决方案
第一种方法 首先是网上普遍见到的 关闭服务 windows下在C:\ProgramData(隐藏文件夹)\MySQL\MySQL Server 5.7\my.ini中的[mysqld]下添加skip- ...
- Docker NGINX 例子
版权所有,未经许可,禁止转载 章节 Docker 介绍 Docker 和虚拟机的区别 Docker 安装 Docker Hub Docker 镜像(image) Docker 容器(container ...