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 ...
随机推荐
- 17. Getting to the essence of things
17.Getting to the essence of things.抓住事情的本质 From today on, I think I should keep a diary. To the CCU ...
- 打开c++ 项目遇到的错误
前言 后续持续更新: 无法打开源文件windows.h https://blog.csdn.net/Mr__George/article/details/87714252 找不到duilib.h ht ...
- python中pandas数据分析基础3(数据索引、数据分组与分组运算、数据离散化、数据合并)
//2019.07.19/20 python中pandas数据分析基础(数据重塑与轴向转化.数据分组与分组运算.离散化处理.多数据文件合并操作) 3.1 数据重塑与轴向转换1.层次化索引使得一个轴上拥 ...
- java代码静态分析工具
需求:有些基础类型的遍历,值希望它被赋予指定的几个值,赋予其他值能够别编译器(IDEA/eclipse)提醒 Android studu结合自己的插件,引入 <!-- https://mvnre ...
- 洛谷训练场——简单模拟 排座位(P1056)
题目描述 上课的时候总会有一些同学和前后左右的人交头接耳,这是令小学班主任十分头疼的一件事情.不过,班主任小雪发现了一些有趣的现象,当同学们的座次确定下来之后,只有有限的D对同学上课时会交头接耳. 同 ...
- 指令——mkdir
一个完整的指令的标准格式: Linux通用的格式——#指令主体(空格) [选项](空格) [操作对象] 一个指令可以包含多个选项,操作对象也可以是多个 指令mkdir——(make directory ...
- P-数学程序猿今天开始写博客了
∧ /| /\7 ≤_/ ∧. | | / / / 〉 | Z_,< / ...
- SQL 、LINQ日前比较
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; ...
- Essay写作如何提升自己的辩驳水平?
辩证思维在英文写作上的表现方式有许多种,今天来讲讲Counterargument&Rebut,广泛用于英文写作和口语辩论.其作用就是通过辩驳和你论点相反的意见,来突出自己的论点更正确. 话说衡 ...
- Day 17:缓冲输出字符流和用缓冲输入输出实现登录、装饰者设计模式
输出字符流 Writer 所有输出字符流的基类, 抽象类. FileWriter 向文件输出字符数据的输出字符流. BufferedWriter 缓冲输出字符流 缓冲输出字符流作用: ...