【问题标题】:Python list remove item in the loop oncePython list 删除循环中的项目一次
【发布时间】:2014-07-26 13:08:59
【问题描述】:

我需要用户通过以下方式输入,

abc item1 item2
abc item3 item4
pqr item2 item3
456 item1 item1
abc
>>> Invalid Input
123 item2 item5
... ..... .....
789

基本上,用户可以多次输入以 abc 开头的行,但在任何其他以 pqr 或 456 或 123 开头的行之前。如果用户在其他行之后输入 abc,则应将其视为无效条目,并且输入将在用户终止时终止将输入 789。

这是我的代码,

    COMMANDS = ['abc','pqr', '123', '456', '789']

    INPUT = []
    for ROW in iter(raw_input, COMMANDS[4]):
      ROW = ROW.split()
      if len(INPUT) == 0:
        if ROW[0] == COMMANDS[0]:
          INPUT.append(ROW)
        else:
          print '>>> Invalid Input'
      elif ROW[0] == COMMANDS[0]:
        INPUT.append(ROW)
      elif ROW[0] in COMMANDS[1:]:
        COMMANDS = COMMANDS[1:]
        INPUT.append(ROW)
      else:
        print '>>> Invalid Input'

    print INPUT

我认为我的第二个 elif 语句有问题,在循环中它只是不断地砍掉列表。我不知道如何使这个工作,请帮忙。如果您认为有更好的方法来做我想做的事情,请提出建议。

这是我运行上述代码时的输出,

    abc
    pqr
    abc
    >>> Invalid Input
    123
    345
    >>> Invalid Input
    456
    abc
    >>> Invalid Input
    pqr
    >>> Invalid Input
    789
    [['abc'], ['pqr'], ['123'], ['456']]

在输出中,您可以看到 pqr 被标记为不应该的无效输入,我认为在我的第二个 efif 中,我正在切掉“abc”,并且随着循环的进行,它也从我不想要的清单。我只想将 abc 从列表中删除。

终于找到了解决办法,

    COMMANDS = ['abc','pqr', '123', '456', '789']

    INPUT = []
    for ROW in iter(raw_input, COMMANDS[4]):
      ROW = ROW.split()
      if len(INPUT) == 0:
        if ROW[0] == COMMANDS[0]:
          INPUT.append(ROW)
        else:
          print '>>> Invalid Input'
      elif ROW[0] == COMMANDS[0]:
        INPUT.append(ROW)
      elif ROW[0] not in COMMANDS:
        print '>>> Invalid Input'
      else:
        try:
          COMMANDS.remove('abc')
        except ValueError, e:
          pass
        INPUT.append(ROW)

    print INPUT

这可能不是最好的方法,但现在为我工作。如果您有更好的方法,请告诉我。

【问题讨论】:

  • 什么不工作,您的程序似乎按描述工作?
  • 用户可以输入多少次以pqr、123、456开头的行?顺序是否相关?
  • 嗨@PadraicCunningham,我已经更新了代码的输出,并且在输出中,pqr 在循环迭代中被截断了。
  • 嗨@miindlek,没有确定的顺序,输入行也是可变的。
  • 您正在从 COMMANDS 中删除 pqr,因此您的代码正在执行应有的操作。你想做什么,应该删除 pqr 还是只删除 abc?

标签: python list loops


【解决方案1】:

试试这个:

commands = ['pqr', '123', '456', '789']
inp = []
user_inp = None
non_repeat = "abc"
while user_inp != "789":
    user_inp = raw_input()
    # if it is the first entry and input == non_repeat 
    if not inp and user_inp == non_repeat:
        inp.append(user_inp)
    # else if all entries so far are == non_repeat and currnet input == non_repeat
    elif user_inp == non_repeat and all(x == non_repeat for x in inp):
        inp.append(user_inp)
     # else if it is a valid command, append
    elif inp and user_inp in commands:
        inp.append(user_inp)
    # else we tried adding non_repeat after other elements or a command not in the list
    else:
        print ">>> Invalid Input"

您应该使用小写的变量名,同时将 cmets 添加到您的代码中将帮助您了解正在发生的事情,并让尝试帮助您的人更容易。

【讨论】:

  • 谢谢@Padraic Cunningham 这看起来更干净了,我最终将我的代码与你的代码混合在一起,现在它工作正常。
猜你喜欢
  • 1970-01-01
  • 2011-11-12
  • 2022-01-03
  • 1970-01-01
  • 2013-04-22
  • 2018-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多