有没有一种方法可以在python中使用for循环在每次迭代中对一个项执行不同的操作

2024-10-01 04:46:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在每次迭代中对一个项目执行不同的操作。用python帮助我完成下面的代码

router1 = "10.11.12.13"
router2 = "10.11.12.14"


tunnel1, tunnel2, tunnel3, tunnel4 = "tunnel01","tunnel02","tunnel03","tunnel04"

router = [router1, router2]
tunnel = [tunnel1, tunnel2, tunnel3, tunnel4]

for each in router:
    for eachtunnel in tunnel:


#Here in the first iteration I have to bring up tunnel1 on router1 and shut remaining tunnels on both router 1 and 2.

#In the second iteration I have to bring up tunnel2 on router1 and shut remaining tunnels on both the routers.

它应该持续到8个迭代,其中只有一个隧道是向上的,其余的是向下的。你知道吗

在本例中,我将每个路由器的隧道数设为4,但可能会有所不同。请建议我如何才能做到这一点。你知道吗


Tags: andtheinforonhaveroutertunnel
2条回答

这应该管用。我会在隧道和路由器上循环两次,如果内环的路由器和隧道与外环的路由器和隧道匹配,我们就补上,否则就补上

def up(tunnel, router):
    print('up', tunnel, router)

def down(tunnel, router):
    print('down', tunnel, router)

for r1 in router:
    for t1 in tunnel:
        for r2 in router:
            for t2 in tunnel:
                if r1 == r2 and t1 == t2:
                    up(r1, t1)
                else:
                    down(r2, t2)

样本输出为

up 10.11.12.13 tunnel01
down 10.11.12.13 tunnel02
down 10.11.12.13 tunnel03
down 10.11.12.13 tunnel04
down 10.11.12.14 tunnel01
down 10.11.12.14 tunnel02
down 10.11.12.14 tunnel03
down 10.11.12.14 tunnel04
down 10.11.12.13 tunnel01
up 10.11.12.13 tunnel02
down 10.11.12.13 tunnel03
down 10.11.12.13 tunnel04
down 10.11.12.14 tunnel01
down 10.11.12.14 tunnel02
down 10.11.12.14 tunnel03
down 10.11.12.14 tunnel04
down 10.11.12.13 tunnel01
down 10.11.12.13 tunnel02
up 10.11.12.13 tunnel03
down 10.11.12.13 tunnel04
down 10.11.12.14 tunnel01
down 10.11.12.14 tunnel02
down 10.11.12.14 tunnel03
down 10.11.12.14 tunnel04
down 10.11.12.13 tunnel01
down 10.11.12.13 tunnel02
down 10.11.12.13 tunnel03
up 10.11.12.13 tunnel04
down 10.11.12.14 tunnel01
down 10.11.12.14 tunnel02
down 10.11.12.14 tunnel03
down 10.11.12.14 tunnel04
down 10.11.12.13 tunnel01
down 10.11.12.13 tunnel02
down 10.11.12.13 tunnel03
down 10.11.12.13 tunnel04
up 10.11.12.14 tunnel01
down 10.11.12.14 tunnel02
down 10.11.12.14 tunnel03
down 10.11.12.14 tunnel04
down 10.11.12.13 tunnel01
down 10.11.12.13 tunnel02
down 10.11.12.13 tunnel03
down 10.11.12.13 tunnel04
down 10.11.12.14 tunnel01
up 10.11.12.14 tunnel02
down 10.11.12.14 tunnel03
down 10.11.12.14 tunnel04
down 10.11.12.13 tunnel01
down 10.11.12.13 tunnel02
down 10.11.12.13 tunnel03
down 10.11.12.13 tunnel04
down 10.11.12.14 tunnel01
down 10.11.12.14 tunnel02
up 10.11.12.14 tunnel03
down 10.11.12.14 tunnel04
down 10.11.12.13 tunnel01
down 10.11.12.13 tunnel02
down 10.11.12.13 tunnel03
down 10.11.12.13 tunnel04
down 10.11.12.14 tunnel01
down 10.11.12.14 tunnel02
down 10.11.12.14 tunnel03
up 10.11.12.14 tunnel04

一个糟糕的列表理解方法将是。你知道吗

[up(r1, t1) if r1 == r2 and t1 == t2 else down(r2, t2) for t1 in tunnel for r1 in router for r2 in router for t2 in tunnel]

还可以使用itertools创建笛卡尔积itertools.product,然后遍历它们。你知道吗

from itertools import product, tee
#Create cartesian production of router and tunnel, and convert to list
prod = product(router, tunnel)
#Convert iterator to list
li = list(prod)

#Loop through both lists and perform up/down accordingly
for r1, t1 in li:
    for r2, t2 in li:
        if r1 == r2 and t1 == t2:
            up(r1, t1)
        else:
            down(r2, t2)

这个怎么样:

routers = [router1, router2]
tunnels = [tunnel1, tunnel2, tunnel3, tunnel4]

# Assuming everything is down at the beginning

for router in routers:
    for tunnel in tunnels:
        up(router, tunnel)
        # do stuff
        down(router, tunnel)

为了便于阅读,我对变量名做了一些修改。你知道吗

相关问题 更多 >