【问题标题】:Popping items from a list using a loop使用循环从列表中弹出项目
【发布时间】:2017-09-13 14:03:02
【问题描述】:

我正在尝试在 python 中编写一个 for 循环来弹出列表中的所有项目,但只有两个,所以我尝试了这个:

guest = ['john', 'phil', 'andy', 'mark', 'frank', 'joe']

for people in guest:
  popped_guest = guest.pop()
  print("I am sorry " + popped_guest + " I can no longer invite you to dinner")

这就是我运行它时得到的结果:

对不起,乔,我不能再邀请你吃饭了

很抱歉弗兰克,我不能再邀请你吃饭了

对不起,马克我不能再邀请你吃饭了

所以它只弹出 3,但有没有办法让它弹出 6 中的 4?我尝试添加一个 if 语句:

guest = ['john', 'phil', 'andy', 'mark', 'frank', 'joe']

for people in guest:
  if people > guest[1]:
    popped_guest = guest.pop()
    print("I am sorry " + popped_guest + " I can no longer invite you to dinner")

我本来以为 'phil' 会是 1,所以它会弹出最后 4 个,但是当我运行程序时它什么也没返回。那么可以在一个for循环中做吗?

【问题讨论】:

  • if people > guest[1] 对您意味着什么?
  • 您正在迭代一个列表,同时对其进行变异。除非您了解自己在做什么以及如何安全地做,否则不要这样做。
  • 你想说那个字符串 > 另一个字符串.. 不能工作
  • @Astrom 好吧,它可以工作,只是没有像 OP 认为的那样......

标签: python python-3.x for-loop


【解决方案1】:

如果你想弹出 4 个东西,那就数到 4

for _ in range(4):
    popped_guest = guest.pop()
    print("I am sorry " + popped_guest + " I can no longer invite you to dinner") 

【讨论】:

  • 这就是解决方案。另外,使用range 更合适,因为我们实际上并不关心正在迭代什么,只关心循环重复了多少次。
【解决方案2】:

您的for 循环在第三次迭代后停止,因为这是在弹出之前的元素后guest 中剩余的元素数。您可以潜在地使用 while 循环来不断弹出元素,直到列表只剩下 2 个。

while len(guest) > 2:
    popped_guest = guest.pop()
    ...

【讨论】:

    【解决方案3】:

    如前所述,您的代码并没有按照您的想法进行操作,因为您在积极迭代列表的同时将元素从列表中弹出。我会说“一个更好的编码实践是复制要弹出的列表”,但这不是一个“更好”的实践——你的方式根本不像你想要的那样工作,它总是会弹出您列表的前半部分。

    我会问自己“我如何指定在我的当前迭代中弹出谁”和“我在哪里设置在我的当前迭代中弹出多少人”。这两个问题的答案似乎都是“我不知道”。

    【讨论】:

      【解决方案4】:

      这种行为的原因是当您在 Python 中使用 for 循环时,它实际上是通过其索引号遍历列表。因此,当您循环遍历一个列表并同时对其进行变异时,您可能会跳过一些元素。更好的方法是在遍历原始列表的同时改变列表的副本。

      【讨论】:

        【解决方案5】:
        invite = ['Pankaj', 'Deepak', 'Nischey', 'Ram', 'Martin', 'Gopal']
        
        for item in invite:
            if invite.index(item) > 1:
                popped_guest = invite.pop()
                print("Sorry you are not invited " + popped_guest)
            else:
                print("you are invited " + item)
        

        【讨论】:

          【解决方案6】:

          让 2 个值仍在列表中。 当列表长度未知时。

          guest = ['john', 'phil', 'andy']
          
          except2 = (len(guest)) - 2
          
          for i in range(except2):
              popped_guest = guest.pop()
              print("I am sorry " + popped_guest + " I can no longer invite you to dinner") 
              
          else:
              for i in guest:
                  print(f"You are invited  {i}")
          
          I am sorry andy I can no longer invite you to dinner
          You are invited  john
          You are invited  phil
          
          [Program finished]
          

          【讨论】:

            【解决方案7】:

            我正在添加一些代码希望它可以帮助你。

            text = ["hello", "world"]
            
            while True:
                try:
                    val = text.pop()
                    print(val)
                except IndexError:
                    return
            

            【讨论】:

              猜你喜欢
              • 2020-10-15
              • 2022-12-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-07-01
              • 1970-01-01
              • 1970-01-01
              • 2021-12-24
              相关资源
              最近更新 更多