Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python从变量(i,20)开始在范围内迭代_Python_List_Loops_For Loop - Fatal编程技术网

Python从变量(i,20)开始在范围内迭代

Python从变量(i,20)开始在范围内迭代,python,list,loops,for-loop,Python,List,Loops,For Loop,Python新手。我不确定我是不是用最好的方式表达了这一点,但还是这样。我有如下命令列表: 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

Python新手。我不确定我是不是用最好的方式表达了这一点,但还是这样。我有如下命令列表:

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",
]
将cmd1之后的5个值与cmd1进行比较,将cmd2之后的5个值与cmd2进行比较,等等。我尝试以以下方式迭代循环,但这似乎并不理想

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
这对第一个循环有效,但当for循环迭代时,我返回到1,从5或6或其他任何值


更好的方法?谢谢,您遇到了一个错误,因为变量index
i
不是迭代器对象。它只是范围索引的一个副本。更改其值不会影响循环

您可以将代码转换为for-each格式,这样就不必担心索引问题。 确保不要将列表推送到与生成器使用的列表相同的列表。 比如说

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
更好的方法

以下是一种更干净的方法:

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

我会把这些都写进字典:

>>> step = 6
>>> commands = {cmd_list[i]: cmd_list[i+1:i+step]
                for i in range(0, len(cmd_list), step)}
然后可以使用命令名编制索引:

>>> 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"]

对于i,枚举(cmd_列表)中的项:
是一种更好的迭代方式<代码>i将从零开始并自动递增
item
是正在迭代的当前项,相当于
cmd\u list[i]
。也就是说,如果您甚至需要
i
;否则,
对于cmd\u列表中的项:
@user2503227是否确定每个cmd只有5个命令?