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 ...
随机推荐
- css滚动
css 滚动transform: translateY(-100px);jquery $(box).height(); //获取元素高度$(box).scrollTop();//获得元素的滚动条高度
- SpringMVC 文件的上传、下载
文件上传 (1)下载添加2个jar包 commons-fileupload.jar commons-io.jar SpringMVC的文件上传依赖于Apache的FileUpload组件,需要下载添加 ...
- select rank() over
select * from (select rank() over(partition by barcode order by sheetdate) num, * from ScanRecord wh ...
- List<string>绑定到DataGridView控件
问题 将一个简单的List<string>作为数据源绑定到 DataGridView myDataGridView.DataSource = myStringList; 但是只得到一个名为 ...
- 如何在ESXi 5.x/6.x(2005205)中下载和安装异步驱动程序
本文提供了在ESXi 5.x和6.x中下载和安装异步驱动程序的步骤 https://kb.vmware.com/s/article/2005205
- 吴裕雄--天生自然java开发常用类库学习笔记:属性类Properties
import java.util.Properties; public class PropertiesDemo01{ public static void main(String args[]){ ...
- 学习进度04(billbill长评数据提取01)
学习了python写入csv文件自己想了一个小实战,爬取billbill<白色相簿>番剧的长评 网页是动态变化的,往下拉他才会更新出长评,找出关键链接https://api.bilibil ...
- jsp与spring mvc后台controller间参数传递处理之总结
在编程过程中,最容易出现问题及卡壳的地方,往往是各层之间接缝处,接缝处往往存在着各种各样的参数传递,数据转换和格式化,参数很好的传递并正确接收过来之后就是复杂逻辑之间的处理了,所以为了避免多种问题占用 ...
- linux下编译bochs-2.6.2
操作系统: Fedora19 内核: 3.12.9 Linux localhost.localdomain 3.12.9-201.fc19.i686 #1 SMP Wed Jan 29 16:02:1 ...
- Spring Boot2(003):简要回顾“HelloWorld” web 工程
1.注解: @RestController 和 @RequestMapping HelloWorldExample 中的第1个注解 @RestController 是一个被熟知的原型注解(stereo ...