鱼C论坛

 找回密码
 立即注册
查看: 2956|回复: 4

[已解决]为什么用remove之后列表只删除前后两个元素

[复制链接]
发表于 2020-2-17 21:04:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
附上代码


sandwich_orders = ["lisa", "ben", "kevin","alice"]

fished_sandwiches = []

for people in sandwich_orders:
    print("hello " + people)

    print(sandwich_orders)

    sandwich_orders.remove(people)

    print(sandwich_orders)

    sandwich = input("which kind sandwich would you like ?")

    print("i mande your " + sandwich + " sanwich")

    fished_sandwiches.append(sandwich)

    print("you sandwich is finished")

print(fished_sandwiches)
print(sandwich_orders)


输出结果:
hello lisa
['lisa', 'ben', 'kevin', 'alice']
['ben', 'kevin', 'alice']
which kind sandwich would you like ?a
i mande your a sanwich
you sandwich is finished


hello kevin
['ben', 'kevin', 'alice']
['ben', 'alice']
which kind sandwich would you like ?b
i mande your b sanwich
you sandwich is finished


['a', 'b']
['ben', 'alice']

Process finished with exit code 0
最佳答案
2020-2-17 21:07:09
remove() 在遍历中使用通常都是陷阱。。

代码帮你改好了:

  1. sandwich_orders = ["lisa", "ben", "kevin", "alice"]
  2. fished_sandwiches = []

  3. for people in sandwich_orders[:]:
  4.     print("hello " + people)
  5.     print(sandwich_orders)
  6.     sandwich_orders.remove(people)
  7.     print(sandwich_orders)
  8.     sandwich = input("which kind sandwich would you like ?")
  9.     print("i mande your " + sandwich + " sanwich")
  10.     fished_sandwiches.append(sandwich)
  11.     print("you sandwich is finished")

  12. print(fished_sandwiches)
  13. print(sandwich_orders)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-17 21:04:49 | 显示全部楼层
新手菜鸟求助
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-17 21:07:09 | 显示全部楼层    本楼为最佳答案   
remove() 在遍历中使用通常都是陷阱。。

代码帮你改好了:

  1. sandwich_orders = ["lisa", "ben", "kevin", "alice"]
  2. fished_sandwiches = []

  3. for people in sandwich_orders[:]:
  4.     print("hello " + people)
  5.     print(sandwich_orders)
  6.     sandwich_orders.remove(people)
  7.     print(sandwich_orders)
  8.     sandwich = input("which kind sandwich would you like ?")
  9.     print("i mande your " + sandwich + " sanwich")
  10.     fished_sandwiches.append(sandwich)
  11.     print("you sandwich is finished")

  12. print(fished_sandwiches)
  13. print(sandwich_orders)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-2-18 19:36:58 | 显示全部楼层
zltzlt 发表于 2020-2-17 21:07
remove() 在遍历中使用通常都是陷阱。。

代码帮你改好了:

谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-18 21:17:46 | 显示全部楼层
  1. a = list(range(10))
  2. i = 0
  3. for b in a:
  4.     print('第' +  str(i) + '次循环a剩余的为',a)
  5.     print('第' +  str(i) + '次循环删除的为' ,b)
  6.     i +=1
  7.     a.remove(b)
复制代码

英语小白  看的英语比代码都费劲  总结下你的大概是这个意思
我也是初学着有不对的地方 望指出
  1. a = list(range(10))
  2. i = 0
  3. for b in a[:]:
  4.     #这里用上a列表的复制  用remove可以达到想要的结果
  5.     print('第' +  str(i) + '次循环a剩余的为',a)
  6.     print('第' +  str(i) + '次循环删除的为' ,b)
  7.     i +=1
  8.     a.remove(b)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-2 21:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表