简体   繁体   中英

Python list remove item in the loop once

I need the user input in following way,

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

Basically the user can input lines starting with abc any number of times but before any other line starts with pqr or 456 or 123. If user enters abc after the other lines it should consider as invalid entry and the input will terminate when user will enter 789.

Here is my code,

    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

I think I have problem with my second elif statement where in the loop it just keep chopping the list. I am not sure how to make this working, please help. If you think there is a better way of doing what I am trying to do then please suggest.

Here is the output when I run the above code,

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

In the output you can see the pqr was marked as Invalid Input which it should not be and I think in my second efif I am chopping the 'abc' and as the loop progressed it chopped the 'pqr' as well from the list which I don't want. I just want abc to be chopped from the list.

Finally managed to find the workaround,

    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

This may not be the best way but for now working for me. Please do let me know if you have better way to do this.

Try this:

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"

You should use lower case for variable names, also adding comments to your code will help you see what is happening and also make it easier for people trying to help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM