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 ...
随机推荐
- C语言循环
C 练习实例1 #include<stdio.h> int main() { int i,j,k; printf("\n"); //此处巧妙的利用循环次数和四个相等的关 ...
- 二次urldecode注入
原理大多数web程序都会对输入字符进行转换,例如addslashes(),mysql_real_escape_string(),mysql_escape_string(),也就是对单引号',双引号&q ...
- HTML<figure> <figcaption> 标签定义图文并茂
本来想分两篇文章来解释说明figure.figcaption的,但是这俩个标签都是定义图文的,所以我们合起来讲解,大家更能容易接受. 大家在写xhtml.html中常常用到一种图片列表,图片+标题 或 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-info-sign
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- Linux学习《第四章脚本》20200222
- wireshark混杂模式
来自:https://blog.csdn.net/mukami0621/article/details/78645825 通过设置网卡为混杂模式就能捕获局域网内所有发包内容,包括非广播包和非发给自己主 ...
- UVA - 816 Abbott's Revenge(bfs)
题意:迷宫从起点走到终点,进入某点的朝向不同,可以出去的方向也不同,输出最短路. 分析:因为朝向决定接下来在该点可以往哪里走,所以每个点需要有三个信息:x,y,d(坐标和进入该点的朝向),所以将起点的 ...
- TX2开发板Ubuntu16.04设置静态IP
TX2开发板Ubuntu16.04设置静态IP https://www.cnblogs.com/qilai/p/11285445.html 首先打开一个Terminal输入 ifconfig 查看自 ...
- 155-PHP stripos函数
<?php $str='password'; //定义一个字符串 $position=strpos($str,'S'); //查找字母s第一次出现的位置 echo '字母S的位置是'.$posi ...
- 3.3. Mapping methods with several source parameters(具有多个源参数的映射方法)
3.3. Mapping methods with several source parameters(具有多个源参数的映射方法) MapStruct 还支持具有多个源参数的映射方法.这是比较实用的, ...