user2503227
user2503227

Reputation: 137

Python Iterate over range starting with variable (i,20)

New to Python. Not sure if i'm expressing this in the best way, but here goes. I have a list of commands like this:

cmd_list = [
"cmd1",
".1.3.6.1.4.1.24391.4.1.3.2.1.2.1.2.1",
".1.3.6.1.4.1.24391.4.1.3.3.1.3.1.4.1",
".1.3.6.1.4.1.24391.4.1.3.2.1.2.1.3.1",
".1.3.6.1.4.1.24391.4.1.3.2.1.2.1.4.1",
".1.3.6.1.4.1.24391.4.1.3.2.1.2.1.5.1", 
"cmd2",
".1.3.6.1.4.1.24391.4.1.3.2.1.2.1.2.1",
".1.3.6.1.4.1.24391.4.1.3.3.1.3.1.4.1",
".1.3.6.1.4.1.24391.4.1.3.2.1.2.1.3.1",
".1.3.6.1.4.1.24391.4.1.3.2.1.2.1.4.11",
".1.3.6.1.4.1.24391.4.1.3.2.1.2.1.5.11",
"cmd3",
".1.3.6.1.4.1.24391.4.1.3.2.1.2.1.2.12",
".1.3.6.1.4.1.24391.4.1.3.3.1.3.1.4.12",
".1.3.6.1.4.1.24391.4.1.3.2.1.2.1.3.12",
".1.3.6.1.4.1.24391.4.1.3.2.1.2.1.4.12",
".1.3.6.1.4.1.24391.4.1.3.2.1.2.1.5.12",
]

The 5 values after cmd1 get compared to cmd1, the 5 after cmd2 with cmd2, etc. I am trying to iterate through the loop in the following way but it doesn't seem ideal.

i=0
for i in range(i,cmd_list.__len__()):
    #expect to first see normal command (check it doesn't start with .)
    i += 1   
    while cmd_list[i].startswith("."):
         #save these values to a list
         i += 1
    #do stuff when I have all the command info

This works for the first one but then when the for loop iterates, i goes back to 1, from 5 or 6 or whatever it was.

Better ways to do this? Thanks

Upvotes: 1

Views: 579

Answers (3)

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24133

I'd put it all into a dictionary:

>>> step = 6
>>> commands = {cmd_list[i]: cmd_list[i+1:i+step]
                for i in range(0, len(cmd_list), step)}

Then you can index using the command name:

>>> commands['cmd2']
[".1.3.6.1.4.1.24391.4.1.3.2.1.2.1.2.1",
 ".1.3.6.1.4.1.24391.4.1.3.3.1.3.1.4.1",
 ".1.3.6.1.4.1.24391.4.1.3.2.1.2.1.3.1",
 ".1.3.6.1.4.1.24391.4.1.3.2.1.2.1.4.11",
 ".1.3.6.1.4.1.24391.4.1.3.2.1.2.1.5.11"]

Upvotes: 1

Sumit
Sumit

Reputation: 2270

You are encountering an error because variable index i is not a iterator object. It is just a copy of index from range. Changing its value won't affect the loop.

You can convert your code in the for each format, so that you don't have to worry about index. Make sure that you do not push to the same list as the list is being used for generator. For Example

commands = []
command = None
for cmd in cmd_list:
    #expect to first see normal command (check it doesn't start with .)
    if cmd.startswith("."):
      #save these values to a list
      commands.append(cmd)
    else:
      if command:
         #do stuff when I have all the command info
         commands = []
      command = cmd

Upvotes: 0

Martin Konecny
Martin Konecny

Reputation: 59611

Better ways to do this?

Here's cleaner one approach:

for e in cmd_list:   
    if e.startswith("."):
         #we have values to save to list
     else:
         # e is cmd1, cmd2 etc.

    #do stuff when I have all the command info

Upvotes: 0

Related Questions