【求助】 while循环 遍历列表
代码:
sandwich_orders = ['apple', 'pastrami', 'pear', 'pastrami', 'water', 'pastrami']
finished_sandwiches =[]
print("Sorry, we dont't have pastrami")
print(sandwich_orders)
while sandwich_orders:
if 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
sandwich_order = sandwich_orders.pop()
print('I made your tuna sandwich ' + sandwich_order)
finished_sandwiches.append(sandwich_order)
print("\nFinish sandwich: ")
for finished_sandwiche in finished_sandwiches:
print(finished_sandwiche)
运行结果:
Sorry, we dont't have pastrami
['apple', 'pastrami', 'pear', 'pastrami', 'water', 'pastrami']
I made your tuna sandwich pastrami
I made your tuna sandwich water
I made your tuna sandwich pear
I made your tuna sandwich apple
Finish sandwich:
pastrami
water
pear
apple
想要是实现的效果是 sandwich_orders 列表中的 'pastrami'都删除,finished_sandwiches列表中不包含 'pastrami' 只改了一个单词,找到它了吗?
sandwich_orders = ['apple', 'pastrami', 'pear', 'pastrami', 'water', 'pastrami']
finished_sandwiches =[]
print("Sorry, we dont't have pastrami")
print(sandwich_orders)
while sandwich_orders:
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
sandwich_order = sandwich_orders.pop()
print('I made your tuna sandwich ' + sandwich_order)
finished_sandwiches.append(sandwich_order)
print("\nFinish sandwich: ")
for finished_sandwiche in finished_sandwiches:
print(finished_sandwiche) 如果非得用while来循环,抱歉,恕我愚钝想不出来,但是用for循环和lambda就很快,代码如下:
sandwich_orders = ['apple', 'pastrami', 'pear', 'pastrami', 'water', 'pastrami']
finished_sandwiches =[]
print("Sorry, we dont't have pastrami")
print(sandwich_orders)
for i in sandwich_orders[:]:
if i == 'pastrami':
sandwich_orders.remove(i)
finished_sandwiches =
print("\nFinish sandwich: ")
print(sandwich_orders)
print(finished_sandwiches) qq1151985918 发表于 2021-6-11 16:33
只改了一个单词,找到它了吗?
请问一下
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
这两句已经遍历删除了吧,那下面这句是什么意思啊。。
sandwich_order = sandwich_orders.pop() 在我打完这段代码时才发现已经有这么多人回答了,,sandwich_orders = ['apple', 'pastrami', 'pear', 'pastrami', 'water', 'pastrami']
finished_sandwiches =[]
print("Sorry, we dont't have pastrami")
print(sandwich_orders)
##while sandwich_orders:
# pop 是后进先出的,remove 是先进先出的,所以while 在执行第一次循环时把sandwich_orders 的最后一个元素
# "pastrami" 添加到了 "finished_sandwiches"
# 你应该这样做
for each_food in sandwich_orders:
if 'pastrami' != each_food: # 如果食材不是牛肉饼才会添加
finished_sandwiches.append(each_food)
print('I made your tuna sandwich ' + each_food)
sandwich_orders = []
print("\nFinish sandwich: ")
for finished_sandwiche in finished_sandwiches:
print(finished_sandwiche) 我不是第一个 发表于 2021-6-12 09:51
请问一下
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
sandwich_order=sandwich_orders列表的最后一项 你画个流程图就好理解了,sandwich_orders在第一次循环的时候,第一项不满足 if 'pastrami' in sandwich_orders:这个条件,里面的remove没有执行,接下来执行了sandwich_order = sandwich_orders.pop(),你sandwich_orders最后一项是pastrami,pop出这一项pastrami就成了sandwich_order的第一个值,此时你又把sandwich_order这个值append进了 finished_sandwiches里面,所以你执行打印时就有pastrami了
页:
[1]