while循环处理列表和字典
for循环时一种遍历列表的有效方式,但在for循环中不应该修改列表,否则将导致python难以跟踪其中的元素。要在遍历列表的同时对其进行修改,可使用while循环。通过while循环同列表和字典结合起来使用,可收集,存储并组织大量输入,供以后查看和显示。
- 在列表之间移动元素
比如有一个列表,其中包含新注册但还未验证的网站用户;验证这些用户后,如何将他们移到另一个已验证用户列表中呢?一种办法是使用一个while循环,在验证用户的同时将其从未验证用户列表中提取出来,再将其加入到另一个已验证用户列表中。
unconfirmed_users = ['alice' , 'brian' , 'candace']
confirmed_users = []
while unconfirmed_users:
current_user = unconfirmed_users.pop()
print("Verifying user: " + current_user.title())
confirmed_users.append(current_user)
print("\nThe following users have been confirmed: ")
for confirmed_user in confirmed_users:
print(confirmed_user.title())
Verifying user: Candace
Verifying user: Brian
Verifying user: Alice
The following users have been confirmed:
Candace
Brian
Alice
------------------
(program exited with code: 0)
Press return to continue
- 删除包含特定值的所有列表元素
在前面章节,说使用方法remove()来删除列表中的特定值,这之所以可行,是因为要删除的值在列表中只出现了一次。如果要删除列表中所有包含特定值的元素,可以使用while循环。
比如:
pets = ['dog' , 'cat' , 'dog' , 'goldfish' , 'cat' , 'rabbit' , 'cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'rabbit']
------------------
(program exited with code: 0)
Press return to continue
- 使用用户输入来填充字典
可以使用while循环提示用户输入任意数量的信息,下面来一个调查程序的例子,其中呢,循环每一次执行时都提示输入被调查者的名字和回答,我们将收集的数据存储在一个字典中,以便将回答同被调查者关联起来:
responses = {}
polling_active = True
while polling_active:
name = input("\nWhat is your name?")
response = input("Which mountain would you like to climb someday?")
responses[name] = response
repeat = input("Would you like to let another person respond?(yes/no)")
if repeat == 'no':
polling_active = False
print("\n----Poll Results------")
for name,response in responses.items():
print(name + "Would like to climb " + response + ".")
What is your name?aa
Which mountain would you like to climb someday?aa
Would you like to let another person respond?(yes/no)yes
What is your name?bb
Which mountain would you like to climb someday?jj
Would you like to let another person respond?(yes/no)yes
What is your name?oo
Which mountain would you like to climb someday?pp
Would you like to let another person respond?(yes/no)no
----Poll Results------
aaWould like to climb aa.
bbWould like to climb jj.
ooWould like to climb pp.
------------------
(program exited with code: 0)
Press return to continue
例子:
print("wu xiang jiang niu rou mai wan liao!")
sandwich_orders = ['hanbao' ,'pastrami', 'regou' , 'pastrami','zhurou' , 'niurou','pastrami']
finished_sandwiches = []
while sandwich_orders:
sandwich_order = sandwich_orders.pop()
# print("I made your " + sandwich_order)
if sandwich_order == 'pastrami':
continue
else:
finished_sandwiches.append(sandwich_order)
for finished_sandwiche in finished_sandwiches:
print(finished_sandwiche)
wu xiang jiang niu rou mai wan liao!
niurou
zhurou
regou
hanbao
------------------
(program exited with code: 0)
Press return to continue
例子:
vocations = {}
select = True
while select:
name = input("plese input your name: ")
vaction = input("your want go to place: ")
vocations[name] = vaction
repeat = input("whether who join,if have:yes,else:no")
if repeat == 'no':
select = False
for name,vaction in vocations.items():
print(name + "want go to " + vaction + "playing")
plese input your name: oo
your want go to place: dd
whether who join,if have:yes,else:noyes
plese input your name: pp
your want go to place: dd
whether who join,if have:yes,else:noyes
plese input your name: rr
your want go to place: gg
whether who join,if have:yes,else:nono
ppwant go to ddplaying
rrwant go to ggplaying
oowant go to ddplaying
------------------
(program exited with code: 0)
Press return to continue