【问题标题】:How to check next iter item without proceeding in For loop in Python如何检查下一个迭代项而不在 Python 中的 For 循环中继续
【发布时间】:2016-06-25 21:41:54
【问题描述】:

当通过next(iter) 进行迭代时,我想基本上将网络组合在一起,我通过计算每行中出现的项目数来做到这一点,如果它有 7 个项目,那就是一个条目的开始。

然后我在输入行下方查找行,直到找到另一行包含 7 个项目。然后该过程重新开始。

唯一的问题是当我通过line = next(bgp_table_iter) 访问“下一行”并中断循环时,如果“下一行”有 7 个项目意味着开始一个新条目,当它从 for line in bgp_table_iter: 开始时它会迭代再次跳到下一行,跳过应该有 7 个项目并且是新条目的开始的原始“下一行”。

哪一端跳过所有其他条目。

如何防止跳过?还是让 For 循环触发器在 break 时转到上一个迭代?

def show_ip_bgp():
    data = test_output_short.split('RPKI validation codes: V valid, I invalid, N Not found\n\n     Network          Next Hop            Metric LocPrf Weight Path\n')
    bgp_table = data[1]
    bgp_table = re.sub("     Network          Next Hop            Metric LocPrf Weight Path\n","",bgp_table)
    bgp_table = re.sub("\*","",bgp_table)

    bgp_table_list = bgp_table.split('\n')

    bgp_table_iter = iter(bgp_table_list)

    overall_dict = {}
    index = 0
    for line in bgp_table_iter:
        # print line.split(), len(line.split())

        current_line = line.split()
        # If 7 items are in the list it's the start of a network
        if len(current_line) == 7:
            best = True if '>' in current_line[0] else False
            network = current_line[1]
            attached_ip = current_line[2]
            print "Found Starting Network", network, attached_ip, "Best Path: {}".format(best)
            for i in range(0,1000):
                line = next(bgp_table_iter)
                subline_line = line.split()
                best_sub = 'True' if '>' in subline_line[0] else 'False'
                if len(subline_line) == 6:
                    print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
                elif len(subline_line) == 5:
                    print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
                elif len(subline_line) == 7:
                    break



test_output_short = '''BGP table version is 3538854, local router ID is 10.15.1.81
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter,
              x best-external, a additional-path, c RIB-compressed,
Origin codes: i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

     Network          Next Hop            Metric LocPrf Weight Path
 * i 10.0.1.0/24      10.8.111.45              0    115      0 i
 *>i                  10.8.11.45               0    120      0 i
 * i                  10.8.11.45               0    120      0 i
 * i                  10.8.11.45               0    120      0 i
 * i 10.0.3.0/29      10.8.111.45              0    115      0 i
 *>i                  10.8.11.45               0    120      0 i
 * i                  10.8.11.45               0    120      0 i
 * i                  10.8.11.45               0    120      0 i
 * i 10.8.0.0/16      10.9.0.1                 0     50      0 i
 * i                  10.8.0.1                 0     50      0 i
 *>                   0.0.0.0                  0         32768 i
 * i 10.9.0.0/16      10.9.0.1                 0     50      0 i
 * i                  10.8.0.1                 0     50      0 i
 *>                   0.0.0.0                  0         32768 i
 *>i 10.10.2.0/24     10.8.10.2                0    100      0 i
 * i                  10.8.10.2                0    100      0 i
 * i                  10.8.10.2                0    100      0 i
 * i 10.10.5.0/24     10.8.142.15              0     85      0 i
 *>i                  10.8.42.15               0    100      0 i
 * i                  10.8.42.15               0    100      0 i
 * i                  10.8.42.15               0    100      0 i
 *>i 10.10.7.0/24     10.8.40.84               0    100      0 i
 * i                  10.8.40.84               0    100      0 i
 * i                  10.8.40.84               0    100      0 i
 *>i 10.10.8.0/24     10.8.10.8                0    100      0 i
 * i                  10.8.110.8               0     85      0 i
 * i                  10.8.10.8                0    100      0 i
 * i                  10.8.10.8                0    100      0 i
 *>i 10.10.11.0/24    10.8.42.8                0    100      0 i
 * i                  10.8.42.8                0    100      0 i
 * i                  10.8.42.8                0    100      0 i
 * i                  10.9.42.8                0    100      0 i
 * i 10.10.12.0/24    10.8.10.12               0    100      0 i
 * i                  10.8.10.12               0    100      0 i
 *>i                  10.8.10.12               0    100      0 i'''

输出:

>>> show_ip_bgp()
Found Starting Network 10.0.1.0/24 10.8.111.45 Best Path: False
Sub: 10.8.11.45 Best Path: True
Sub: 10.8.11.45 Best Path: False
Sub: 10.8.11.45 Best Path: False
Found Starting Network 10.8.0.0/16 10.9.0.1 Best Path: False
Sub: 10.8.0.1 Best Path: False
Sub: 0.0.0.0 Best Path: True
Found Starting Network 10.10.2.0/24 10.8.10.2 Best Path: True
Sub: 10.8.10.2 Best Path: False
Sub: 10.8.10.2 Best Path: False
Found Starting Network 10.10.7.0/24 10.8.40.84 Best Path: True
Sub: 10.8.40.84 Best Path: False
Sub: 10.8.40.84 Best Path: False
Found Starting Network 10.10.11.0/24 10.8.42.8 Best Path: True
Sub: 10.8.42.8 Best Path: False
Sub: 10.8.42.8 Best Path: False
Sub: 10.9.42.8 Best Path: False

更新:

在我运行测试之前,建议的修复似乎有效。它确实使所有网络看起来有点。

def show_ip_bgp(): data = test_output_very_short.split('RPKI 验证码:V 有效,I 无效,N 未找到\n\n Network Next Hop Metric LocPrf Weight Path\n') bgp_table = 数据[1] bgp_table = re.sub("网络下一跳度量 LocPrf 权重路径\n","",bgp_table) bgp_table = re.sub("*","",bgp_table)

bgp_table_list = bgp_table.split('\n')

bgp_table_iter = iter(bgp_table_list)

overall_dict = {}
index = 0
for i, line in enumerate(bgp_table_list):
    # print line.split(), len(line.split())

    current_line = line.split()
    # If 7 items are in the list it's the start of a network
    if len(current_line) == 7:
        best = True if '>' in current_line[0] else False
        network = current_line[1]
        attached_ip = current_line[2]
        print "Found Starting Network", network, attached_ip, "Best Path: {}".format(best)
        for i in range(0,1000):
            line = bgp_table_list[i+1]
            print i
            subline_line = line.split()
            best_sub = 'True' if '>' in subline_line[0] else 'False'
            if len(subline_line) == 6:
                print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
            elif len(subline_line) == 5:
                print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
            elif len(subline_line) == 7:
                line = bgp_table_list[i+1]
                break

输出:

Found Entry Network:  10.0.1.0/24 Nexthop: 10.8.111.45 Best Path: False Pref: 115
0
Found Network:  10.0.1.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.0.1.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.0.1.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.0.3.0/29 Nexthop: 10.8.111.45 Best Path: False Pref: 115
0
Found Network:  10.0.3.0/29 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.0.3.0/29 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.0.3.0/29 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.8.0.0/16 Nexthop: 10.9.0.1 Best Path: False Pref: 50
0
Found Network:  10.8.0.0/16 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.8.0.0/16 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.8.0.0/16 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.9.0.0/16 Nexthop: 10.9.0.1 Best Path: False Pref: 50
0
Found Network:  10.9.0.0/16 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.9.0.0/16 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.9.0.0/16 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.2.0/24 Nexthop: 10.8.10.2 Best Path: True Pref: 100
0
Found Network:  10.10.2.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.2.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.2.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.5.0/24 Nexthop: 10.8.142.15 Best Path: False Pref: 85
0
Found Network:  10.10.5.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.5.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.5.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.7.0/24 Nexthop: 10.8.40.84 Best Path: True Pref: 100
0
Found Network:  10.10.7.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.7.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.7.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.8.0/24 Nexthop: 10.8.10.8 Best Path: True Pref: 100
0
Found Network:  10.10.8.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.8.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.8.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.11.0/24 Nexthop: 10.8.42.8 Best Path: True Pref: 100
0
Found Network:  10.10.11.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.11.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.11.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.12.0/24 Nexthop: 10.8.10.12 Best Path: False Pref: 100
0
Found Network:  10.10.12.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.12.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.12.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.15.0/24 Nexthop: 10.8.10.15 Best Path: False Pref: 100
0
Found Network:  10.10.15.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.15.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.15.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.27.0/24 Nexthop: 10.8.41.81 Best Path: False Pref: 100
0
Found Network:  10.10.27.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.27.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.27.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3

每次显示这里的索引号都会回到0,打印出正在检查的行的索引。

【问题讨论】:

    标签: python python-2.7 for-loop iterator next


    【解决方案1】:

    不要将其强制为iter,而是继续将其用作list,然后使用enumerate 来了解您在list 中的位置。

    编辑:更改第一个枚举以使用 index,第二个循环的范围以 1 而不是 0 开头,并检查到 line = bgp_table_list[index+i] 的行,并在到达文件末尾时使用 except 子句。

    for index, line in enumerate(bgp_table_list):
        # print line.split(), len(line.split())
    
        current_line = line.split()
        # If 7 items are in the list it's the start of a network
        if len(current_line) == 7:
            best = True if '>' in current_line[0] else False
            network = current_line[1]
            attached_ip = current_line[2]
            print "Found Starting Network", network, attached_ip, "Best Path: {}".format(best)
            for i in range(1, 1000):
                try:
                    line = bgp_table_list[index+i]
                except IndexError:
                    break
                subline_line = line.split()
                best_sub = 'True' if '>' in subline_line[0] else 'False'
                if len(subline_line) == 6:
                    print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
                elif len(subline_line) == 5:
                    print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
                elif len(subline_line) == 7:
                    break
    

    我的输出(不确定是否正确,提供给你判断)

    Found Starting Network 10.0.1.0/24 10.8.111.45 Best Path: False
    Sub: 10.8.11.45 Best Path: True
    Sub: 10.8.11.45 Best Path: False
    Sub: 10.8.11.45 Best Path: False
    Found Starting Network 10.0.3.0/29 10.8.111.45 Best Path: False
    Sub: 10.8.11.45 Best Path: True
    Sub: 10.8.11.45 Best Path: False
    Sub: 10.8.11.45 Best Path: False
    Found Starting Network 10.8.0.0/16 10.9.0.1 Best Path: False
    Sub: 10.8.0.1 Best Path: False
    Sub: 0.0.0.0 Best Path: True
    Found Starting Network 10.9.0.0/16 10.9.0.1 Best Path: False
    Sub: 10.8.0.1 Best Path: False
    Sub: 0.0.0.0 Best Path: True
    Found Starting Network 10.10.2.0/24 10.8.10.2 Best Path: True
    Sub: 10.8.10.2 Best Path: False
    Sub: 10.8.10.2 Best Path: False
    Found Starting Network 10.10.5.0/24 10.8.142.15 Best Path: False
    Sub: 10.8.42.15 Best Path: True
    Sub: 10.8.42.15 Best Path: False
    Sub: 10.8.42.15 Best Path: False
    Found Starting Network 10.10.7.0/24 10.8.40.84 Best Path: True
    Sub: 10.8.40.84 Best Path: False
    Sub: 10.8.40.84 Best Path: False
    Found Starting Network 10.10.8.0/24 10.8.10.8 Best Path: True
    Sub: 10.8.110.8 Best Path: False
    Sub: 10.8.10.8 Best Path: False
    Sub: 10.8.10.8 Best Path: False
    Found Starting Network 10.10.11.0/24 10.8.42.8 Best Path: True
    Sub: 10.8.42.8 Best Path: False
    Sub: 10.8.42.8 Best Path: False
    Sub: 10.9.42.8 Best Path: False
    Found Starting Network 10.10.12.0/24 10.8.10.12 Best Path: False
    Sub: 10.8.10.12 Best Path: False
    Sub: 10.8.10.12 Best Path: True
    

    【讨论】:

    • 这会产生不同的问题,现在开始条目下方的地址块出现在不正确的条目中。查看更新。
    • 你还有line = next(bgp_table_iter),导致了这个问题。我的意思是用line = bgp_table_list[i+1]替换它。
    • 仍然不正确,注意 10.8.11.45 只是重复为相同的子网。
    • line = bgp_table_list[i+1] 正在抓取for range 的索引
    • 哎呀,看到修改后的答案,我认为这就是你想要的。
    猜你喜欢
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多