# 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 循环来处理列表和字典的更多相关文章

  1. 使用while循环来处理列表和字典——参考Python编程从入门到实践

    1. 在列表之间移动元素 unconfirmed_users = ['alice', 'brian', 'candace'] confirmed_users = [] # 验证每个用户,知道没有未验证 ...

  2. input函数以及while处理列表和字典

    一.函数input()的工作原理 .input()函数:获取输入的字符串 示例: message = input('请输入信息,方便电脑显示') print(message) print('您输入的信 ...

  3. python基础之循环结构以及列表

    python基础之编译器选择,循环结构,列表 本节内容 python IDE的选择 字符串的格式化输出 数据类型 循环结构 列表 简单购物车的编写 1.python IDE的选择 IDE的全称叫做集成 ...

  4. Bash实用技巧:同时循环两个列表

    摘要: 你会学到一种原创的同时循环两个列表的方法.类似于Python或者Haskell的zip函数,非常简洁直观,效果如下: $ paste <( ) <( ) | while read ...

  5. 第五篇:python基础之循环结构以及列表

    python基础之循环结构以及列表   python基础之编译器选择,循环结构,列表 本节内容 python IDE的选择 字符串的格式化输出 数据类型 循环结构 列表 简单购物车的编写 1.pyth ...

  6. vue.js循环for(列表渲染)详解

    vue.js循环for(列表渲染)详解 一.总结 一句话总结: v-for <ul id="example-1"> <li v-for="item in ...

  7. Python基础、判断、循环、列表、字典,day1

    一.Python 简介 1.介绍 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标 ...

  8. join,列表和字典用for循环的删除,集合,深浅拷贝

    1.join() 将列表转换成字符串,并且每个字符之间用另一个字符连接起来,join后面必须是可迭代的对象(字符串,列表,元组,字典,集合),数字不能迭代 例如: s = ['a','b','c'] ...

  9. python中for循环里去修改列表注意的事项

    你的微信好友当中有 5 个推销的,他们存在一个列表 # black_list=['卖茶叶', '卖面膜', '卖保险', '卖花生', '卖手机'] # 当中, 请把这 5 个人分别从 black_l ...

随机推荐

  1. 移动端 三段式布局 (flex方式)

    分享一种平时用的三段式布局(flex) 主要思路是  上中下    header&footer 给高度  main 占其余部分 html 部分 <div class='wrap'> ...

  2. dedeCMS网站栏目管理---释义

    一.常规选项 文件保存目录:可以指定保存的目录,也可以用拼音,指定为拼音时系统会自动生成栏目拼音的目录,文件保存目录一般用拼音即可. 目录相对位置:有上级目录,CMS目录与站点根目录.实际上指的都是网 ...

  3. maven知识结构笔记

    1.什么是maven Maven 翻译为"专家"."内行",是 Apache 下的一个纯 Java 开发的开源项目.基于项目对象模型(缩写:POM)概念,Mav ...

  4. openalyser6学习

    1.安装nvm.nvm下载地址:https://github.com/coreybutler/nvm-windows/releases使用nvm-setup安装和下载nodejs:https://ww ...

  5. 【学CG系列】web之审查元素

    一.审查元素的作用 审查元素(你的F12)可以做到定位网页元素.实时监控网页元素属性变化的功能,可以及时调试.修改.定位.追踪检查.查看嵌套 ,修改样式和查看js动态输出信息,是开发人员得心应手的好工 ...

  6. Ubuntu安装Python版本管理工具pyenv

    gyf@gyf-VirtualBox:~$ git clone https://github.com/yyuu/pyenv.git ~/.pyenvCloning into '/home/gyf/.p ...

  7. netty权威指南学习笔记三——TCP粘包/拆包之粘包现象

    TCP是个流协议,流没有一定界限.TCP底层不了解业务,他会根据TCP缓冲区的实际情况进行包划分,在业务上,一个业务完整的包,可能会被TCP底层拆分为多个包进行发送,也可能多个小包组合成一个大的数据包 ...

  8. JS 三大难点

    1,作用域链 2,原型链 3,闭包

  9. 指令——cat

    作用1:cat有直接打开一个文件的功能,只看不用编辑. 语法:#cat 文件的路径 选项:-n, --number 对输出的所有行编号. 如:用cat查看/etc/passwd [root@local ...

  10. 01-JAVA语言基础——课后动手动脑

    1.一个java类文件中真的只能有一个公有类吗? 请使用Eclipse或javac检测一下以下代码,有错吗? public class Test{    public static void main ...